蔭獲得空指針異常增加則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沒有得到我哪裏有做任何錯誤。請幫助我解決這個問題。
我有一個doubt.is TabActivity沒有deprecated.Eclipse提出了警告,當我試圖 – vinoth
現在TabActivity已經過時 – user1213202