Search code examples
javaandroidnullpointerexceptionandroid-tabhost

Getting Null Pointer on Reference to TabHost (Android)


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.


Solution

  • 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);
    
    
    }