我正在构建一个使用 tabs 实现 Bluetooth、Wifi、Phone call 和 Sms 的应用程序。MainActivity.java 文件如下所示。
package com.example.servicesdemo;
import android.os.Bundle;
import android.app.Activity;
import android.app.TabActivity;
import android.content.Intent;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;
import android.support.v4.app.NavUtils;
public class MainActivity extends TabActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TabHost tabHost = getTabHost();
TabSpec btspec = tabHost.newTabSpec("Bluetooth");
btspec.setIndicator("Bluetooth");
Intent btIntent = new Intent(getBaseContext() , BtActivity.class);
btspec.setContent(btIntent);
TabSpec wifispec = tabHost.newTabSpec("Wifi");
wifispec.setIndicator("Wifi");
Intent wifiIntent = new Intent(getBaseContext(),WifiActivity.class);
wifispec.setContent(wifiIntent);
TabSpec callspec = tabHost.newTabSpec("Phone Call");
callspec.setIndicator("Phone Call");
Intent callIntent = new Intent(getBaseContext(),CallActivity.class);
wifispec.setContent(callIntent);
TabSpec smsspec = tabHost.newTabSpec("SMS");
wifispec.setIndicator("SMS");
Intent smsIntent = new Intent(getBaseContext(),SmsActivity.class);
smsspec.setContent(smsIntent);
tabHost.addTab(btspec);
tabHost.addTab(wifispec);
tabHost.addTab(callspec);
tabHost.addTab(smsspec);
tabHost.setCurrentTab(0);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
我创建了 4 个 XML 文件和 4 个 Activity 文件(上面提到的每个服务一个)。例如,我为蓝牙选项卡创建了一个 BtActivity.java 文件,如下所示(稍后我将编写更多代码)。
BtActivity.java
package com.example.servicesdemo;
import android.app.Activity;
import android.os.Bundle;
public class BtActivity extends Activity{
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.bt_layout);
}
}
现在,当我尝试运行应用程序时,它只会崩溃。LogCat 错误如下所示。
08-29 10:44:33.810: D/AndroidRuntime(369): Shutting down VM
08-29 10:44:33.810: W/dalvikvm(369): threadid=1: thread exiting with uncaught exception (group=0x40015560)
08-29 10:44:33.830: E/AndroidRuntime(369): FATAL EXCEPTION: main
08-29 10:44:33.830: E/AndroidRuntime(369): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.servicesdemo/com.example.servicesdemo.MainActivity}: java.lang.IllegalArgumentException: you must specify a way to create the tab content
08-29 10:44:33.830: E/AndroidRuntime(369): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
08-29 10:44:33.830: E/AndroidRuntime(369): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
08-29 10:44:33.830: E/AndroidRuntime(369): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
08-29 10:44:33.830: E/AndroidRuntime(369): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
08-29 10:44:33.830: E/AndroidRuntime(369): at android.os.Handler.dispatchMessage(Handler.java:99)
08-29 10:44:33.830: E/AndroidRuntime(369): at android.os.Looper.loop(Looper.java:130)
08-29 10:44:33.830: E/AndroidRuntime(369): at android.app.ActivityThread.main(ActivityThread.java:3683)
08-29 10:44:33.830: E/AndroidRuntime(369): at java.lang.reflect.Method.invokeNative(Native Method)
08-29 10:44:33.830: E/AndroidRuntime(369): at java.lang.reflect.Method.invoke(Method.java:507)
08-29 10:44:33.830: E/AndroidRuntime(369): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
08-29 10:44:33.830: E/AndroidRuntime(369): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
08-29 10:44:33.830: E/AndroidRuntime(369): at dalvik.system.NativeStart.main(Native Method)
08-29 10:44:33.830: E/AndroidRuntime(369): Caused by: java.lang.IllegalArgumentException: you must specify a way to create the tab content
08-29 10:44:33.830: E/AndroidRuntime(369): at android.widget.TabHost.addTab(TabHost.java:202)
08-29 10:44:33.830: E/AndroidRuntime(369): at com.example.servicesdemo.MainActivity.onCreate(MainActivity.java:43)
08-29 10:44:33.830: E/AndroidRuntime(369): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
08-29 10:44:33.830: E/AndroidRuntime(369): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
08-29 10:44:33.830: E/AndroidRuntime(369): ... 11 more
08-29 10:49:53.781: D/AndroidRuntime(378): Shutting down VM
08-29 10:49:53.781: W/dalvikvm(378): threadid=1: thread exiting with uncaught exception (group=0x40015560)
08-29 10:49:53.790: E/AndroidRuntime(378): FATAL EXCEPTION: main
08-29 10:49:53.790: E/AndroidRuntime(378): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.servicesdemo/com.example.servicesdemo.MainActivity}: java.lang.IllegalArgumentException: you must specify a way to create the tab content
08-29 10:49:53.790: E/AndroidRuntime(378): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
08-29 10:49:53.790: E/AndroidRuntime(378): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
08-29 10:49:53.790: E/AndroidRuntime(378): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
08-29 10:49:53.790: E/AndroidRuntime(378): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
08-29 10:49:53.790: E/AndroidRuntime(378): at android.os.Handler.dispatchMessage(Handler.java:99)
08-29 10:49:53.790: E/AndroidRuntime(378): at android.os.Looper.loop(Looper.java:130)
08-29 10:49:53.790: E/AndroidRuntime(378): at android.app.ActivityThread.main(ActivityThread.java:3683)
08-29 10:49:53.790: E/AndroidRuntime(378): at java.lang.reflect.Method.invokeNative(Native Method)
08-29 10:49:53.790: E/AndroidRuntime(378): at java.lang.reflect.Method.invoke(Method.java:507)
08-29 10:49:53.790: E/AndroidRuntime(378): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
08-29 10:49:53.790: E/AndroidRuntime(378): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
08-29 10:49:53.790: E/AndroidRuntime(378): at dalvik.system.NativeStart.main(Native Method)
08-29 10:49:53.790: E/AndroidRuntime(378): Caused by: java.lang.IllegalArgumentException: you must specify a way to create the tab content
08-29 10:49:53.790: E/AndroidRuntime(378): at android.widget.TabHost.addTab(TabHost.java:202)
08-29 10:49:53.790: E/AndroidRuntime(378): at com.example.servicesdemo.MainActivity.onCreate(MainActivity.java:43)
08-29 10:49:53.790: E/AndroidRuntime(378): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
08-29 10:49:53.790: E/AndroidRuntime(378): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
08-29 10:49:53.790: E/AndroidRuntime(378): ... 11 more
08-29 10:49:56.110: I/Process(378): Sending signal. PID: 378 SIG: 9
我之前遇到过类似的问题,但在那种情况下,我试图在选项卡中显示的一个类没有扩展 Activity 类(这是用户告诉我的)。
这是我上一个问题的链接以供参考。
但是现在,我已经使用了所有扩展 Activity 类的类,所以在运行时应该没有问题。请帮忙