我正在使用本教程制作一个标签应用程序。当我在设备上运行我的应用程序时,它会给我强制关闭消息。这是我的主要活动代码,我的 logcat 在下面。我不知道我在哪里犯错。这是代码
public class TabLayoutActivity extends TabActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Resources res = getResources(); // Resource object to get Drawables
TabHost tabHost = getTabHost(); // The activity TabHost
TabHost.TabSpec spec; // Resusable TabSpec for each tab
Intent intent; // Reusable Intent for each tab
//Artist Tab
intent = new Intent(this, Artists.class);
spec = tabHost.newTabSpec("artists").setIndicator("Artist", res.getDrawable(R.drawable.tab_icon)).setContent(intent);
tabHost.addTab(spec);
//Songs
intent = new Intent(this, Songs.class);
spec = tabHost.newTabSpec("songs").setIndicator("Songs", res.getDrawable(R.drawable.tab_icon)).setContent(intent);
tabHost.addTab(spec);
//Albums
intent = new Intent(this, Album.class);
spec = tabHost.newTabSpec("artists").setIndicator("Artist", res.getDrawable(R.drawable.tab_icon)).setContent(intent);
tabHost.addTab(spec);
tabHost.setCurrentTab(1);
}
}
这是我的日志猫
05-28 19:16:51.421: D/dalvikvm(15189): GC_EXTERNAL_ALLOC freed 43K, 50% free 2725K/5379K, external 0K/0K, paused 33ms
05-28 19:16:51.441: D/AndroidRuntime(15189): Shutting down VM
05-28 19:16:51.441: W/dalvikvm(15189): threadid=1: thread exiting with uncaught exception (group=0x4001e578)
05-28 19:16:51.451: E/AndroidRuntime(15189): FATAL EXCEPTION: main
05-28 19:16:51.451: E/AndroidRuntime(15189): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.zafar.tablayout/com.zafar.tablayout.TabLayoutActivity}: java.lang.RuntimeException: Your content must have a TabHost whose id attribute is 'android.R.id.tabhost'
05-28 19:16:51.451: E/AndroidRuntime(15189): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651)
05-28 19:16:51.451: E/AndroidRuntime(15189): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
05-28 19:16:51.451: E/AndroidRuntime(15189): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
05-28 19:16:51.451: E/AndroidRuntime(15189): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
05-28 19:16:51.451: E/AndroidRuntime(15189): at android.os.Handler.dispatchMessage(Handler.java:99)
05-28 19:16:51.451: E/AndroidRuntime(15189): at android.os.Looper.loop(Looper.java:130)
05-28 19:16:51.451: E/AndroidRuntime(15189): at android.app.ActivityThread.main(ActivityThread.java:3691)
05-28 19:16:51.451: E/AndroidRuntime(15189): at java.lang.reflect.Method.invokeNative(Native Method)
05-28 19:16:51.451: E/AndroidRuntime(15189): at java.lang.reflect.Method.invoke(Method.java:507)
05-28 19:16:51.451: E/AndroidRuntime(15189): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:907)
05-28 19:16:51.451: E/AndroidRuntime(15189): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:665)
05-28 19:16:51.451: E/AndroidRuntime(15189): at dalvik.system.NativeStart.main(Native Method)
05-28 19:16:51.451: E/AndroidRuntime(15189): Caused by: java.lang.RuntimeException: Your content must have a TabHost whose id attribute is 'android.R.id.tabhost'
05-28 19:16:51.451: E/AndroidRuntime(15189): at android.app.TabActivity.onContentChanged(TabActivity.java:105)
05-28 19:16:51.451: E/AndroidRuntime(15189): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:218)
05-28 19:16:51.451: E/AndroidRuntime(15189): at android.app.Activity.setContentView(Activity.java:1663)
05-28 19:16:51.451: E/AndroidRuntime(15189): at com.zafar.tablayout.TabLayoutActivity.onCreate(TabLayoutActivity.java:14)
05-28 19:16:51.451: E/AndroidRuntime(15189): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
05-28 19:16:51.451: E/AndroidRuntime(15189): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
05-28 19:16:51.451: E/AndroidRuntime(15189): ... 11 more
05-28 19:16:57.256: I/Process(15189): Sending signal. PID: 15189 SIG: 9
更新
这是我在 main.xml 中的标签主机
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tabHost"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
第二次更新
将 tabHost 更改为 tabhost 后的 Logcat
05-28 19:47:56.271: I/Process(16575): Sending signal. PID: 16575 SIG: 9
05-28 19:47:58.381: D/dalvikvm(16782): GC_EXTERNAL_ALLOC freed 46K, 50% free 2725K/5379K, external 0K/0K, paused 20ms
05-28 19:47:58.411: D/AndroidRuntime(16782): Shutting down VM
05-28 19:47:58.411: W/dalvikvm(16782): threadid=1: thread exiting with uncaught exception (group=0x4001e578)
05-28 19:47:58.411: E/AndroidRuntime(16782): FATAL EXCEPTION: main
05-28 19:47:58.411: E/AndroidRuntime(16782): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.zafar.tablayout/com.zafar.tablayout.TabLayoutActivity}: java.lang.RuntimeException: Your TabHost must have a TabWidget whose id attribute is 'android.R.id.tabs'
05-28 19:47:58.411: E/AndroidRuntime(16782): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651)
05-28 19:47:58.411: E/AndroidRuntime(16782): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
05-28 19:47:58.411: E/AndroidRuntime(16782): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
05-28 19:47:58.411: E/AndroidRuntime(16782): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
05-28 19:47:58.411: E/AndroidRuntime(16782): at android.os.Handler.dispatchMessage(Handler.java:99)
05-28 19:47:58.411: E/AndroidRuntime(16782): at android.os.Looper.loop(Looper.java:130)
05-28 19:47:58.411: E/AndroidRuntime(16782): at android.app.ActivityThread.main(ActivityThread.java:3691)
05-28 19:47:58.411: E/AndroidRuntime(16782): at java.lang.reflect.Method.invokeNative(Native Method)
05-28 19:47:58.411: E/AndroidRuntime(16782): at java.lang.reflect.Method.invoke(Method.java:507)
05-28 19:47:58.411: E/AndroidRuntime(16782): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:907)
05-28 19:47:58.411: E/AndroidRuntime(16782): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:665)
05-28 19:47:58.411: E/AndroidRuntime(16782): at dalvik.system.NativeStart.main(Native Method)
05-28 19:47:58.411: E/AndroidRuntime(16782): Caused by: java.lang.RuntimeException: Your TabHost must have a TabWidget whose id attribute is 'android.R.id.tabs'
05-28 19:47:58.411: E/AndroidRuntime(16782): at android.widget.TabHost.setup(TabHost.java:114)
05-28 19:47:58.411: E/AndroidRuntime(16782): at android.widget.TabHost.setup(TabHost.java:163)
05-28 19:47:58.411: E/AndroidRuntime(16782): at android.app.TabActivity.onContentChanged(TabActivity.java:109)
05-28 19:47:58.411: E/AndroidRuntime(16782): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:218)
05-28 19:47:58.411: E/AndroidRuntime(16782): at android.app.Activity.setContentView(Activity.java:1663)
05-28 19:47:58.411: E/AndroidRuntime(16782): at com.zafar.tablayout.TabLayoutActivity.onCreate(TabLayoutActivity.java:14)
05-28 19:47:58.411: E/AndroidRuntime(16782): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
05-28 19:47:58.411: E/AndroidRuntime(16782): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
05-28 19:47:58.411: E/AndroidRuntime(16782): ... 11 more