【问题标题】:nullpointer exception in setting up a tabhost at calling addTab( TabHost.TabSpec spec) method在调用 addTab(TabHost.TabSpec spec) 方法时设置 tabhost 时出现 nullpointer 异常
【发布时间】:2012-05-23 12:14:00
【问题描述】:

这个问题真的很奇怪,让我发疯。 我无法摆脱 nullPointerException。 我的任务很简单,只需在 tabhost 上运行测试即可。但现在我什至无法创建一个标签主机! 为了创建一个tabhost,我需要注意一些要点。 首先必须有一个 id 为“@android:id/tabs”的 tabwidget 和一个 id 为“@android:id/tabconent”的框架布局。我发誓我确实注意了,您可以在下面的代码中看到这一点。 我阅读了使用 TabActivity 推荐的文章。但不幸的是,这个 tabhost 将成为活动的一个组成部分。所以我必须在正常活动中创建它。

当代码转到“addSpec()”时,它会抛出一个 nullPointerException。我检查了 Logcat 中的 tabHost 和 Host.Spec。他们都不是空的!我会将我的打印结果添加到此问题的末尾。

这是我的 java 代码。

public class Cao extends Activity {
ScrollView menu_scroll;
TabHost menu_host;
TabWidget menu_tags;
int[] contents;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.testtab);
    menu_host = (TabHost) findViewById(R.id.tabhost);
    Log.e("tabhost", (menu_host == null) + "");
    menu_tags = (TabWidget) findViewById(android.R.id.tabs);
    contents = new int[] { R.id.b1, R.id.b2 };
    int count = 6;
    for (int i = 0; i < count; i++) {
        TabSpec spec = menu_host.newTabSpec(i + "");
        spec.setIndicator("no " + i);
        Log.e("spec", (spec == null) + "");
        spec.setContent(R.id.b1); **// the problem occurs at this line**
        menu_host.addTab(spec);
    }
}
}

而我的xml代码如下

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

<TabHost
    android:id="@+id/tabhost"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="50dp"
        android:orientation="vertical" >

        <HorizontalScrollView
            android:id="@+id/menu_scroll"
            android:layout_width="fill_parent"
            android:layout_height="50dp"
            android:scrollbars="none" >

            <TabWidget
                android:id="@android:id/tabs"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content" >
            </TabWidget>
        </HorizontalScrollView>
    </LinearLayout>

    <FrameLayout
        android:id="@android:id/tabcontent"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >

        <Button
            android:id="@+id/b1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="1" />

        <Button
            android:id="@+id/b2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="2" />

    </FrameLayout>
</TabHost>

</LinearLayout>

另外还有 logcat 打印输出

tabhost(490): false
spec(490): false
E/AndroidRuntime(490): Uncaught handler: thread main exiting due to uncaught exception  
E/AndroidRuntime(490): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.tim.wirelessorder/com.tim.wirelessorder.ui.Cao}: java.lang.NullPointerException   
E/AndroidRuntime(490):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496)
E/AndroidRuntime(490):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
E/AndroidRuntime(490):  at android.app.ActivityThread.access$2200(ActivityThread.java:119)
E/AndroidRuntime(490):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
E/AndroidRuntime(490):  at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime(490):  at android.os.Looper.loop(Looper.java:123)
E/AndroidRuntime(490):  at android.app.ActivityThread.main(ActivityThread.java:4363)
E/AndroidRuntime(490):  at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(490):  at java.lang.reflect.Method.invoke(Method.java:521)
E/AndroidRuntime(490):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
E/AndroidRuntime(490):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
E/AndroidRuntime(490):  at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime(490): Caused by: java.lang.NullPointerException    
E/AndroidRuntime(490):  at android.widget.TabHost$ViewIdContentStrategy.<init>(TabHost.java:583)
E/AndroidRuntime(490):  at android.widget.TabHost$ViewIdContentStrategy.<init>(TabHost.java:578)
E/AndroidRuntime(490):  at android.widget.TabHost$TabSpec.setContent(TabHost.java:435)
E/AndroidRuntime(490):  at com.tim.wirelessorder.ui.Cao.onCreate(Cao.java:38)
E/AndroidRuntime(490):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
E/AndroidRuntime(490):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)

【问题讨论】:

    标签: android nullpointerexception android-tabhost android-tabs


    【解决方案1】:

    //把你的tabhost id改成

    <TabHost 
        android:id="@android:id/tabhost"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
    

    //你的Activity像下面这样扩展了TabActivity

    public class HelloTabWidget extends TabActivity {
    

    //在你的 onCreate 中这样做

     TabHost tabHost = getTabHost();  // The activity TabHost
        TabHost.TabSpec spec;  // Resusable TabSpec for each tab
        Intent intent;  // Reusable Intent for each tab
    

    check this:

    【讨论】:

    【解决方案2】:

    你需要扩展 TabActivity 而不是简单的活动 试试

    public class Cao extends TabActivity 
    

    而不是

    public class Cao extends Activity 
    

    然后改变这个

    menu_host = (TabHost) findViewById(R.id.tabhost);
    

    到 menu_host =getTabHost(); 和

     android:id="@+id/tabhost"
    

    android:id="@android:id/tabhost"
    

    【讨论】:

    • 不需要使用 TabActivity。我终于在这个链接stackoverflow.com/questions/3163884/…找到答案了。谢谢大家。
    • 你从来没有说过你需要创建而不扩展 tabactivity 很高兴知道你的问题已经解决了
    【解决方案3】:

    我所需要的只是改变我在 .xml 中声明 TabHost 的 ID 的方式。我没有使用"@+id/tabhost",而是使用了"@android:id/tabhost"。然后,在代码中分配我的TabHost 时,我调用了findViewById(android.R.id.tabhost)。无论出于何种原因,这解决了我的问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-08-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-14
      • 2015-04-23
      相关资源
      最近更新 更多