我正在尝试使用下面的标签主机创建标签式视图,但应用程序被强制关闭...我正在尝试使用标签主机创建标签式视图,如下面但应用程序被强制关闭...我正在尝试创建标签式视图使用下面的标签主机,但应用程序被强制关闭...
MainActivity.java
package darpan.slider;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TabHost;
public class MainActivity extends Activity {
TabHost tabHost;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tabHost = (TabHost) findViewById(R.id.tabHost);
tabHost.setup();
TabHost.TabSpec tab1 = tabHost.newTabSpec("First");
TabHost.TabSpec tab2 = tabHost.newTabSpec("Second");
TabHost.TabSpec tab3 = tabHost.newTabSpec("Third");
tab1.setIndicator("First");
tab1.setContent(new Intent(this, FirstTab.class));
tab2.setIndicator("Second");
tab2.setContent(new Intent(this, SecondTab.class));
tab3.setIndicator("Third");
tab3.setContent(new Intent(this, ThirdTab.class));
tabHost.addTab(tab1);
tabHost.addTab(tab2);
tabHost.addTab(tab3);
tabHost.setCurrentTab(1);
}
}
logcat的:
02-10 15:54:00.061 23783-23783/darpan.slider W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x41c7c700)
02-10 15:54:00.061 23783-23783/darpan.slider E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{darpan.slider/darpan.slider.MainActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2295)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2349)
at android.app.ActivityThread.access$700(ActivityThread.java:159)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1316)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:176)
at android.app.ActivityThread.main(ActivityThread.java:5419)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1046)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at android.widget.TabHost.addTab(TabHost.java:253)
at darpan.slider.MainActivity.onCreate(MainActivity.java:36)
at android.app.Activity.performCreate(Activity.java:5372)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1104)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2257)