Java 获取TabHost(Android)引用的空指针

Java 获取TabHost(Android)引用的空指针,java,android,nullpointerexception,android-tabhost,Java,Android,Nullpointerexception,Android Tabhost,这是过去一天一直困扰我的事情。所以我一直得到一个指向mTabHost的空指针引用,它是onCreate()中的一个变量。以下是相关代码: 从我的主要活动: @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); instance = this; setContentView(R.layout.second_activity);

这是过去一天一直困扰我的事情。所以我一直得到一个指向mTabHost的空指针引用,它是onCreate()中的一个变量。以下是相关代码:

从我的主要活动:

@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());

}
下面是第二个_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>

实际的异常是在我运行时引发的 mTabHost.setup(这是getSupportFragmentManager()),因为mTabHost为空

我注意到一件奇怪的事情,就是Support4Demos中提供的示例碎片选项卡对我也不起作用。我很确定这告诉我我的android-support-v4.jar或我的环境有问题,但我尝试重新下载jar并重新编译,但仍然没有任何效果:尽管编译很好,但我的项目和示例项目在运行时都失败了。至于其他可能的环境问题,我不确定它们可能是什么

如有任何建议,将不胜感激

谢谢, 安德鲁。

试试看

@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);


}

如果您将其更改为:

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


因为在找到第一个布局文件中的对象之前,您正在使用不同的布局调用setContentView?

我不确定是否会出现这种情况。但在使用
Fragment/FragmentHost/FragmentActivity


如果您使用的是android.app.Fragment,请记住使用
Activity
;如果您使用的是
android.support.v4.app.Fragment
,请使用
FragmentActivity
。切勿将
android.support.v4.app.Fragment
FragmentHost
附加到
android.app.Activity
,因为这将导致引发
异常

android.R.id.tabhost
中删除
android.
,我知道Eclipse需要它。请看我对Tarsem回答的回答是你的类扩展了
FragmentActivity
?不,我不相信这是有效的,因为如果你注意到second_activity.xml,给FragmentTabHost的id是\@android:id/tabhost,因此找不到R.id.tabhost作为有效的R.id。我尝试将\@android:id/tabhost更改为简单\@+id/tabhost,然后用R.id.tabhost引用它,但这导致了相同的nullpointerexception。我没有该导入。我正在从我自己的包中导入R类。我尝试了您更新的答案,现在mTabHost返回了它应该返回的内容!但是,mViewPager现在返回null。你知道为什么findViewById不能在我的代码中正常工作吗?你是在扩展SherlockFragmentActivity吗?不,我不是,只是FragmentActivity。我要看看我是否能再接受你的想法。如果我因为它而让它工作,我会用答案标记你的回答。