在标签应用中强制关闭消息

时间:2012-05-28 16:22:17

标签: java android

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

这是我的logcat

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

<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

5 个答案:

答案 0 :(得分: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必须有一个TabWidget,其id属性是'android.R.id.tabs'”。

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

答案 1 :(得分: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"

答案 2 :(得分:0)

您必须导入您的包名.R;喜欢

  

import package.R;

并且也是这样的,我更喜欢用它来获得标签主机。就像是。 `

  

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

并继续帮助

答案 3 :(得分:0)

<强>解决方案

问题出在main.xml文件中。我应该在main.xml中到处使用@android:id/idname。但我用@+id/idname这是错误的。它现在正在运作。

答案 4 :(得分:0)

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

1.需要在manifestfile中给予许可。   2.与当前仿真器或屏幕相比,某些时间布局高度和宽度太大