TabHost.addTab - >空指针异常

时间:2013-05-16 05:47:32

标签: android tabwidget

在向tabhost.i添加tabspec时,Iam得到Nullpointer异常已尝试过这样的

public class MainActivity extends FragmentActivity implements TabHost.TabContentFactory {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        TabHost tabHost= (TabHost) findViewById(R.id.tabhost);
        tabHost.setup();
        TabSpec TextTabSpec = tabHost.newTabSpec("Text");
        TextTabSpec.setIndicator("", getResources().getDrawable(R.drawable.phone_1));
        TextTabSpec.setContent(this);

        TabSpec LogTabSpec = tabHost.newTabSpec("Favourites");
        LogTabSpec.setIndicator("", getResources().getDrawable(R.drawable.call_group));
        LogTabSpec.setContent(this);

        TabSpec ConTabSpec = tabHost.newTabSpec("contacts");
        ConTabSpec.setIndicator("", getResources().getDrawable(R.drawable.message));
        ConTabSpec.setContent(this);

        tabHost.addTab(TextTabSpec);
        tabHost.addTab(LogTabSpec);
        tabHost.addTab(ConTabSpec);
        tabHost.setCurrentTab(0);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public View createTabContent(String tag) {
        // TODO Auto-generated method stub
        return null;
    }

}

我的logcat是

05-16 11:08:29.650: E/AndroidRuntime(655): FATAL EXCEPTION: main
05-16 11:08:29.650: E/AndroidRuntime(655): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.tabsample/com.example.tabsample.MainActivity}: java.lang.NullPointerException
05-16 11:08:29.650: E/AndroidRuntime(655):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
05-16 11:08:29.650: E/AndroidRuntime(655):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
05-16 11:08:29.650: E/AndroidRuntime(655):  at android.app.ActivityThread.access$2300(ActivityThread.java:125)
05-16 11:08:29.650: E/AndroidRuntime(655):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
05-16 11:08:29.650: E/AndroidRuntime(655):  at android.os.Handler.dispatchMessage(Handler.java:99)
05-16 11:08:29.650: E/AndroidRuntime(655):  at android.os.Looper.loop(Looper.java:123)
05-16 11:08:29.650: E/AndroidRuntime(655):  at android.app.ActivityThread.main(ActivityThread.java:4627)
05-16 11:08:29.650: E/AndroidRuntime(655):  at java.lang.reflect.Method.invokeNative(Native Method)
05-16 11:08:29.650: E/AndroidRuntime(655):  at java.lang.reflect.Method.invoke(Method.java:521)
05-16 11:08:29.650: E/AndroidRuntime(655):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
05-16 11:08:29.650: E/AndroidRuntime(655):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
05-16 11:08:29.650: E/AndroidRuntime(655):  at dalvik.system.NativeStart.main(Native Method)
05-16 11:08:29.650: E/AndroidRuntime(655): Caused by: java.lang.NullPointerException
05-16 11:08:29.650: E/AndroidRuntime(655):  at android.widget.TabHost$FactoryContentStrategy.getContentView(TabHost.java:622)
05-16 11:08:29.650: E/AndroidRuntime(655):  at android.widget.TabHost.setCurrentTab(TabHost.java:323)
05-16 11:08:29.650: E/AndroidRuntime(655):  at android.widget.TabHost.addTab(TabHost.java:213)
05-16 11:08:29.650: E/AndroidRuntime(655):  at com.example.tabsample.MainActivity.onCreate(MainActivity.java:33)
05-16 11:08:29.650: E/AndroidRuntime(655):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
05-16 11:08:29.650: E/AndroidRuntime(655):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
05-16 11:08:29.650: E/AndroidRuntime(655):  ... 11 more

我没有得到我犯错的地方。请任何人帮我解决这个问题。

2 个答案:

答案 0 :(得分:1)

最好使用这个。

TabHost tabhost=(TabHost)findViewById(R.id.tabhost);
        tabhost.setup();
        TabHost.TabSpec tabsep=tabhost.newTabSpec("tag1");  // this one needed
        tabsep.setContent(R.id.tab1);
        tabsep.setIndicator("Clock");
        tabhost.addTab(tabsep);

        tabsep=tabhost.newTabSpec("tag2");
        tabsep.setContent(R.id.tab2);
        tabsep.setIndicator("Button");
        tabhost.addTab(tabsep);
        tabhost.setCurrentTab(0);

答案 1 :(得分:0)

扩展超类TabActivity并获取TabHost tabHost = getTabHost();像那样

public class TabHost_Activity extends TabActivity {

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tab_host_);

      Resources resource=getResources();
     TabHost tabHost=getTabHost();
TabSpec tabSpecAndroid = tabHost.newTabSpec("Android").setIndicator("", resource.getDrawable(R.drawable.icon_android_config)).setContent(this);

tabHost.addTab(tabSpecAndroid);
}
}