Java 在tab应用程序中强制关闭消息

Java 在tab应用程序中强制关闭消息,java,android,Java,Android,我正在用这个做一个标签应用程序。当我在设备上运行我的应用程序时,它会给我一条强制关闭消息。这是我的主要活动代码,下面是我的日志。我不知道我在哪里犯了错误。这是密码 public class TabLayoutActivity extends TabActivity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { s

我正在用这个做一个标签应用程序。当我在设备上运行我的应用程序时,它会给我一条强制关闭消息。这是我的主要活动代码,下面是我的日志。我不知道我在哪里犯了错误。这是密码

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

<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" >

是否已将正确的id添加到main.xml中的tabhost

<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添加到main.xml中的tabhost

<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">


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布局文件会更容易,这样我们就可以看到所有可能的错误。

在您的
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布局文件,将更容易,这样我们就可以看到所有可能的错误。

您必须导入包名。R;像

进口包装.R

另外,我更喜欢用这个来获取选项卡主机。像这样的东西`

TabHost th=(TabHost)findviewbyd(R.id.TabHost)


然后继续,我会帮你的

你必须输入你的包裹名称;像

进口包装.R

另外,我更喜欢用这个来获取选项卡主机。像这样的东西`

TabHost th=(TabHost)findviewbyd(R.id.TabHost)


并继续下去,这将有助于解决问题


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

解决方案


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

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

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

需要遵循的一些主要事项

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

我这样做了,然后我得到了这个错误
错误:错误:找不到与给定名称匹配的资源(在'id'处,值为'@android:id/tabHost')。
@al0ne我不确定id是否区分大小写,你试过用tabhost代替tabhost吗?我改了。现在我有了新的错误。你能看到我上面的更新吗。我在第二次更新中发布了logcat我确实发布了logcat,然后我收到了这个错误
错误:错误:找不到与给定名称匹配的资源(在'id'处,值为'@android:id/tabHost')。
@al0ne我不确定该id是否区分大小写,您是否尝试使用tabHost而不是tabHost我更改了它。现在我有了新的错误。你能看到我上面的更新吗。我在第二次更新中发布了logcat
<TabWidget
        android:id="@android:id/tabs"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />