布局文件:<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >    <TabHost android:layout_width="fill_parent"
             android:layout_height="fill_parent"
             android:id="@+id/thid">
        
    </TabHost>
    <TextView android:layout_width="match_parent"
             android:layout_height="match_parent"
             android:id="@+id/tv1"
             android:text="text1"/>
    <TextView android:layout_width="match_parent"
             android:layout_height="match_parent"
             android:id="@+id/tv2"
             android:text="text2"/>
    <TextView android:layout_width="match_parent"
             android:layout_height="match_parent"
             android:id="@+id/tv3"
             android:text="text3"/>
</RelativeLayout>activity:
 public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tabspace);
        
        tabHost = (TabHost)this.findViewById(R.id.thid);
        
        
        
        ts1 = tabHost.newTabSpec("tab1");
        ts2 = tabHost.newTabSpec("tab2");
        ts3 = tabHost.newTabSpec("tab3");
        
        ts1.setContent(R.id.tv1);//这执行这行代码的时候报错
        ts2.setContent(R.id.tv2);
        ts3.setContent(R.id.tv3);
        
        tabHost.addTab(ts1);
        tabHost.addTab(ts2);
        tabHost.addTab(ts3); 
    }报错信息:11-01 12:26:28.085: E/AndroidRuntime(299): FATAL EXCEPTION: main
11-01 12:26:28.085: E/AndroidRuntime(299): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.flp.control/com.flp.control.TabspaceActivity}: java.lang.NullPointerException
11-01 12:26:28.085: E/AndroidRuntime(299):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
11-01 12:26:28.085: E/AndroidRuntime(299):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
11-01 12:26:28.085: E/AndroidRuntime(299):  at android.app.ActivityThread.access$2300(ActivityThread.java:125)
11-01 12:26:28.085: E/AndroidRuntime(299):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
11-01 12:26:28.085: E/AndroidRuntime(299):  at android.os.Handler.dispatchMessage(Handler.java:99)
11-01 12:26:28.085: E/AndroidRuntime(299):  at android.os.Looper.loop(Looper.java:123)
11-01 12:26:28.085: E/AndroidRuntime(299):  at android.app.ActivityThread.main(ActivityThread.java:4627)
11-01 12:26:28.085: E/AndroidRuntime(299):  at java.lang.reflect.Method.invokeNative(Native Method)
11-01 12:26:28.085: E/AndroidRuntime(299):  at java.lang.reflect.Method.invoke(Method.java:521)
11-01 12:26:28.085: E/AndroidRuntime(299):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
11-01 12:26:28.085: E/AndroidRuntime(299):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
11-01 12:26:28.085: E/AndroidRuntime(299):  at dalvik.system.NativeStart.main(Native Method)
11-01 12:26:28.085: E/AndroidRuntime(299): Caused by: java.lang.NullPointerException
11-01 12:26:28.085: E/AndroidRuntime(299):  at android.widget.TabHost$ViewIdContentStrategy.<init>(TabHost.java:586)
11-01 12:26:28.085: E/AndroidRuntime(299):  at android.widget.TabHost$ViewIdContentStrategy.<init>(TabHost.java:581)
11-01 12:26:28.085: E/AndroidRuntime(299):  at android.widget.TabHost$TabSpec.setContent(TabHost.java:438)
11-01 12:26:28.085: E/AndroidRuntime(299):  at com.flp.control.TabspaceActivity.onCreate(TabspaceActivity.java:27)
11-01 12:26:28.085: E/AndroidRuntime(299):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
11-01 12:26:28.085: E/AndroidRuntime(299):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
11-01 12:26:28.085: E/AndroidRuntime(299):  ... 11 more
11-01 12:30:22.085: E/AndroidRuntime(323): FATAL EXCEPTION: main
11-01 12:30:22.085: E/AndroidRuntime(323): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.flp.control/com.flp.control.TabspaceActivity}: java.lang.NullPointerException
11-01 12:30:22.085: E/AndroidRuntime(323):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
11-01 12:30:22.085: E/AndroidRuntime(323):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
11-01 12:30:22.085: E/AndroidRuntime(323):  at android.app.ActivityThread.access$2300(ActivityThread.java:125)
11-01 12:30:22.085: E/AndroidRuntime(323):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
11-01 12:30:22.085: E/AndroidRuntime(323):  at android.os.Handler.dispatchMessage(Handler.java:99)
11-01 12:30:22.085: E/AndroidRuntime(323):  at android.os.Looper.loop(Looper.java:123)
11-01 12:30:22.085: E/AndroidRuntime(323):  at android.app.ActivityThread.main(ActivityThread.java:4627)
11-01 12:30:22.085: E/AndroidRuntime(323):  at java.lang.reflect.Method.invokeNative(Native Method)
11-01 12:30:22.085: E/AndroidRuntime(323):  at java.lang.reflect.Method.invoke(Method.java:521)
11-01 12:30:22.085: E/AndroidRuntime(323):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
11-01 12:30:22.085: E/AndroidRuntime(323):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
11-01 12:30:22.085: E/AndroidRuntime(323):  at dalvik.system.NativeStart.main(Native Method)
11-01 12:30:22.085: E/AndroidRuntime(323): Caused by: java.lang.NullPointerException
11-01 12:30:22.085: E/AndroidRuntime(323):  at android.widget.TabHost$ViewIdContentStrategy.<init>(TabHost.java:586)
11-01 12:30:22.085: E/AndroidRuntime(323):  at android.widget.TabHost$ViewIdContentStrategy.<init>(TabHost.java:581)
11-01 12:30:22.085: E/AndroidRuntime(323):  at android.widget.TabHost$TabSpec.setContent(TabHost.java:438)
11-01 12:30:22.085: E/AndroidRuntime(323):  at com.flp.control.TabspaceActivity.onCreate(TabspaceActivity.java:27)
11-01 12:30:22.085: E/AndroidRuntime(323):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
11-01 12:30:22.085: E/AndroidRuntime(323):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
11-01 12:30:22.085: E/AndroidRuntime(323):  ... 11 more我很奇怪的是,怎么会抛空指针呢,我用debug看了,R.id.tv1是有值的,ts1也不为空,不知道这是怎么回事,请老师们指点下
android

解决方案 »

  1.   

    android Tabhost部件
    TabHost的xml和初始化都有一定的要求,具体楼主可以看看sdk和上面这个链接。
    代码帮你简单改了一下,不会报空指针异常了,楼主再根据自己的需求改改吧。
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >    <TabHost
            android:id="@+id/thid"
            android:layout_width="match_parent"
            android:layout_height="match_parent" >        <TabWidget
                android:id="@android:id/tabs"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" >
            </TabWidget>        <FrameLayout
                android:id="@android:id/tabcontent"
                android:layout_width="match_parent"
                android:layout_height="match_parent" >            <TextView
                    android:id="@+id/tv1"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:text="text1" />            <TextView
                    android:id="@+id/tv2"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:text="text2" />            <TextView
                    android:id="@+id/tv3"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:text="text3" />
            </FrameLayout>
        </TabHost></RelativeLayout> public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_tabspace); TabHost tabHost = (TabHost) this.findViewById(R.id.thid);
    tabHost.setup(); TabSpec ts1 = tabHost.newTabSpec("tab1");
    TabSpec ts2 = tabHost.newTabSpec("tab2");
    TabSpec ts3 = tabHost.newTabSpec("tab3");

    ts1.setIndicator("tab1");
    ts2.setIndicator("tab2");
    ts3.setIndicator("tab3"); ts1.setContent(R.id.tv1);// 这执行这行代码的时候报错
    ts2.setContent(R.id.tv2);
    ts3.setContent(R.id.tv3); tabHost.addTab(ts1);
    tabHost.addTab(ts2);
    tabHost.addTab(ts3);
    }