1

我正在使用本教程制作一个标签应用程序。当我在设备上运行我的应用程序时,它会给我强制关闭消息。这是我的主要活动代码,我的 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
4

5 回答 5

1

在您main.xml的 中,TabHost 的 ID 是什么?

一定是android:id="@android:id/tabhost"

编辑

TabWidget必须定义为:

<TabWidget
        android:id="@android:id/tabs"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />

从 logcat 中其实很容易看出:“你的 TabHost 必须有一个 id 属性为 'android.R.id.tabs' 的 TabWidget”。

如果您发布整个 xml 布局文件会更容易,因此我们可以看到所有可能的错误。

于 2012-05-28T16:26:24.977 回答
0

您是否在 main.xml 中的 tabhost 中添加了正确的 id?

<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/tabhost"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

你错过了id的问题,你必须使用@android,只需替换该行

android:id="@+id/tabHost"

android:id="@android:id/tabhost"
于 2012-05-28T16:25:31.823 回答
0

你必须导入你的包名.R; 像

导入包.R;

并且还以这种方式安装,我更喜欢使用它来获取标签主机。就像是。`

TabHost th = (TabHost)findViewbyid(R.id.tabhost);

并继续下去

于 2012-05-28T17:03:22.920 回答
0

解决方案

问题出在 main.xml 文件中。我应该@android:id/idname在 main.xml 的任何地方都使用过。但是我用@+id/idname了哪个是错误的。它现在正在工作。

于 2012-05-28T17:04:33.183 回答
0

您需要遵循的一些主要事项..

1.需要在manifestfile中给予权限。2.与当前模拟器或屏幕比较时,有时布局高度和宽度太大

于 2012-05-29T05:11:47.583 回答