我在一个活动中有两个标签。当我使用两个选项卡运行应用程序时,它显示强制关闭错误。当我通过注释tabHost.addTab(spec)
wifisettings 选项卡的行来运行相同的应用程序时,时钟设置选项卡正在打开。但我需要打开两个选项卡,即 wifi 设置和时钟设置。
代码在这里:
setContentView(R.layout.preferences);
pref_close = (Button) findViewById(R.id.close_prefs);
TabHost tabHost = getTabHost(); // The activity TabHost
TabHost.TabSpec spec; // Resusable TabSpec for each tab
Intent intent; // Reusable Intent for each tab
// Create an Intent to launch an Activity for the tab (to be reused)
intent = new Intent( WifiManager.ACTION_PICK_WIFI_NETWORK);
// Initialize a TabSpec for each tab and add it to the TabHost
spec = tabHost.newTabSpec("wifi").setIndicator("Wifi Settings",null).setContent(intent);
tabHost.addTab(spec);
// Do the same for the other tabs
intent = new Intent(this, AppSettings.class);
spec = tabHost.newTabSpec("settings").setIndicator(" Clock Settings",null).setContent(intent);
tabHost.addTab(spec);
tabHost.setCurrentTab(0);
pref_close.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
finish();
}
});
}
}
相同的代码适用于设备(Android3.0.1),但不适用于设备(4.1.1)。我的日志是:
03-30 07:47:35.921: E/AndroidRuntime(8712): FATAL EXCEPTION: main
03-30 07:47:35.921: E/AndroidRuntime(8712): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.visionexceed.timeclock/com.vision.timeclock.activity.Preferences}: java.lang.IllegalArgumentException: you must specify a way to create the tab content
03-30 07:47:35.921: E/AndroidRuntime(8712): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2097)
03-30 07:47:35.921: E/AndroidRuntime(8712): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2122)
03-30 07:47:35.921: E/AndroidRuntime(8712): at android.app.ActivityThread.access$600(ActivityThread.java:140)
03-30 07:47:35.921: E/AndroidRuntime(8712): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1228)
03-30 07:47:35.921: E/AndroidRuntime(8712): at android.os.Handler.dispatchMessage(Handler.java:99)
03-30 07:47:35.921: E/AndroidRuntime(8712): at android.os.Looper.loop(Looper.java:137)
03-30 07:47:35.921: E/AndroidRuntime(8712): at android.app.ActivityThread.main(ActivityThread.java:4895)
03-30 07:47:35.921: E/AndroidRuntime(8712): at java.lang.reflect.Method.invokeNative(Native Method)
03-30 07:47:35.921: E/AndroidRuntime(8712): at java.lang.reflect.Method.invoke(Method.java:511)
03-30 07:47:35.921: E/AndroidRuntime(8712): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:994)
03-30 07:47:35.921: E/AndroidRuntime(8712): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:761)
03-30 07:47:35.921: E/AndroidRuntime(8712): at dalvik.system.NativeStart.main(Native Method)
03-30 07:47:35.921: E/AndroidRuntime(8712): Caused by: java.lang.IllegalArgumentException: you must specify a way to create the tab content
03-30 07:47:35.921: E/AndroidRuntime(8712): at android.widget.TabHost.addTab(TabHost.java:231)
03-30 07:47:35.921: E/AndroidRuntime(8712): at com.visionexceed.timeclock.activity.Preferences.onCreate(Preferences.java:35)
03-30 07:47:35.921: E/AndroidRuntime(8712): at android.app.Activity.performCreate(Activity.java:5163)
03-30 07:47:35.921: E/AndroidRuntime(8712): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
03-30 07:47:35.921: E/AndroidRuntime(8712): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2061)