2013-05-16 53 views
0

蔭獲得空指針異常增加則tabspec時tabhost.i試圖這樣TabHost.addTab - >空指針異常

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 

IAM沒有得到我哪裏有做任何錯誤。請幫助我解決這個問題。

回答

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); 
} 
} 
+0

我有一個doubt.is TabActivity沒有deprecated.Eclipse提出了警告,當我試圖 – vinoth

+0

現在TabActivity已經過時 – user1213202

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); 
+0

感謝responding.but沒有變化,如果我使用像也 – user1213202

+0

延伸到位超類活動FragmentActivity實現了TabHost.TabContentFactory。我希望它能工作 –

+0

請不要將新的答案發布爲新帖子,您隨時可以編輯您的帖子以添加其他解決方案。 –