0

This is something that's been bugging me for the past day now. So I keep getting a null pointer reference to mTabHost, a variable in onCreate(). Here's the pertinent code:

From my main activity:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    instance = this;
    setContentView(R.layout.second_activity);

    // Initializing ViewPager and TabHost objects:
    mViewPager = new ViewPager(this);
    mViewPager.setId(R.id.viewpager);
    setContentView(mViewPager);
    mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
    mTabHost.setup(this,getSupportFragmentManager());

}

And here is second_activity.xml:

<?xml version="1.0" encoding="UTF-8"?>

<android.support.v4.app.FragmentTabHost
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/tabhost"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="bottom">

        <fragment android:name="com.yolostudios.dots.main.login.LoginFragment"
            android:id="@+id/login_fragment"
            android:layout_weight="2"
            android:layout_width="match_parent"
            android:layout_height="0dip"
            android:visibility="gone"/>

        <TabWidget
            android:id="@android:id/tabs"
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"/>

        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_weight="0"/>

        <android.support.v4.view.ViewPager
            android:id="@+id/viewpager"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="8" >

            <include
                android:layout_width="fill_parent"
                android:layout_height="60dip"
                android:visibility="invisible"
                android:layout_gravity="bottom"
                android:focusable="true"
                layout="@layout/createspotbutton" />

        </android.support.v4.view.ViewPager>

        <!-- Loading header of this UI which is coded separately -->

        <FrameLayout
            android:id="@+id/toolbar_container"
            android:layout_width="match_parent"
            android:layout_height="50dp" >

            <include
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:padding="20dp"
                layout="@layout/toolbar" />

        </FrameLayout>

    </LinearLayout>

</android.support.v4.app.FragmentTabHost>

The actual exception is thrown when I run mTabHost.setup(this,getSupportFragmentManager()) as mTabHost is null.

Something strange I have noticed is that even the sample FragmentTabs given in Support4Demos does not work for me. I am pretty sure that this tells me something is wrong with my android-support-v4.jar or my environment but I have tried re-downloading the jar and re-compiling but still nothing works: despite compiling fine, both my project and the sample project fail at runtime. As for other possible environment issues, I am not sure what they could be.

Any suggestions would be appreciated.

Thanks, Andrew.

3
  • 1
    Remove android. from android.R.id.tabhost, I know Eclipse wants it there.
    – Vikram
    Jul 23, 2013 at 4:07
  • Please see my response to Tarsem's answer
    – 123
    Jul 23, 2013 at 4:19
  • Is your class extending FragmentActivity?
    – Vikram
    Jul 23, 2013 at 4:45

3 Answers 3

0

try

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    instance = this;
    setContentView(R.layout.second_activity);
    mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
    mTabHost.setup(this,getSupportFragmentManager());

    // Initializing ViewPager and TabHost objects:
    mViewPager = new ViewPager(this);
    mViewPager.setId(R.id.viewpager);
    setContentView(mViewPager);


}
5
  • No, I don't believe that works because if you'll notice in second_activity.xml, the id that is given to the FragmentTabHost is \@android:id/tabhost, and so R.id.tabhost is not found as a valid R.id. I tried changing \@android:id/tabhost to a simple \@+id/tabhost and then referencing it with R.id.tabhost, but that resulted in the same nullpointerexception.
    – 123
    Jul 23, 2013 at 4:18
  • I do not have that import. I am importing the R class from my own package.
    – 123
    Jul 23, 2013 at 4:21
  • I tried your updated answer, and now mTabHost is returning with what it should! However, mViewPager is now returning null. Do you have any idea why findViewById is not working as it should in my code?
    – 123
    Jul 23, 2013 at 5:02
  • are you extending SherlockFragmentActivity ? Jul 23, 2013 at 5:10
  • No I'm not, just FragmentActivity. I'm going to see if I can run with your idea some more. If I get it working because of it I'll mark your response with the answer.
    – 123
    Jul 23, 2013 at 5:22
0

Maybe if you change it to :

setContentView(R.layout.second_activity);
mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);

// Initializing ViewPager and TabHost objects:
mViewPager = new ViewPager(this);
mViewPager.setId(R.id.viewpager);
setContentView(mViewPager);
mTabHost.setup(this,getSupportFragmentManager());

}

Since you're calling setContentView with a different layout before finding the object that's within the first layout file?

0

I am not sure this may be the case. But keep this below message in mind when using Fragment/FragmentHost/FragmentActivity

remember use Activity if you are using android.app.Fragment; use FragmentActivity if you are using android.support.v4.app.Fragment. Never attach a android.support.v4.app.Fragment or FragmentHost to a android.app.Activity, as this will cause an Exception to be thrown.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.