您的内容必须有一个TabHost,其id属性为'android.R.id.tabhost'

Mat*_*ini 8 android android-tabhost android-logcat

我有个问题:

Java代码

public class VisualizzaListaActivity extends TabActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    TabHost tabHost = getTabHost();  // The activity TabHost
    TabHost.TabSpec spec;  // Reusable TabSpec for each tab
    Intent intent;  // Reusable Intent for each tab

    // Create an Intent to launch an Activity for the tab (to be reused)
    intent = new Intent().setClass(this, DaAcquistareActivity.class);

    // Initialize a TabSpec for each tab and add it to the TabHost
    spec = tabHost.newTabSpec("daAcquistare").setIndicator("Da Acquistare").setContent(intent);
    tabHost.addTab(spec);

    // Do the same for the other tabs
    intent = new Intent().setClass(this, AcquistatiActivity.class);
    spec = tabHost.newTabSpec("acquistati").setIndicator("Acquistati").setContent(intent);
    tabHost.addTab(spec);

    tabHost.setCurrentTab(0);
}

}
Run Code Online (Sandbox Code Playgroud)

XML代码

<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost"   <-------------- It's tabhost -.-"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

<LinearLayout
   android:orientation="vertical"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   android:padding="5dp">
    <TabWidget
       android:id="@android:id/tabs"
       android:layout_width="fill_parent"
       android:layout_height="wrap_content" />
    <FrameLayout
       android:id="@android:id/tabcontent"
       android:layout_width="fill_parent"
       android:layout_height="fill_parent"
       android:padding="5dp" />
</LinearLayout>
</TabHost>
Run Code Online (Sandbox Code Playgroud)

和LogCat

12-16 15:26:22.519: E/AndroidRuntime(8262): java.lang.RuntimeException: Unable to start activity ComponentInfo{android.smile.matteo.spesaPRO/android.smile.matteo.spesaPRO.VisualizzaListaActivity}: java.lang.RuntimeException: Your content must have a TabHost whose id attribute is 'android.R.id.tabhost'             
12-16 15:26:22.519: E/AndroidRuntime(8262): Caused by: java.lang.RuntimeException: Your content must have a TabHost whose id attribute is 'android.R.id.tabhost'
12-16 15:26:22.519: E/AndroidRuntime(8262):         at android.smile.matteo.spesaPRO.VisualizzaListaActivity.onCreate(VisualizzaListaActivity.java:13)
Run Code Online (Sandbox Code Playgroud)

问题

有人可以告诉我为什么会说

您的内容必须有一个TabHost,其id属性为'android.R.id.tabhost'

当android:id = @android:id/tabhost?

小智 20

我相信这个消息意味着:

     <TabHost android:id="@+id/tabhost"
Run Code Online (Sandbox Code Playgroud)

应改为:

     <TabHost android:id="@android:id/tabhost"
Run Code Online (Sandbox Code Playgroud)

  • 我不得不使用:android:id ="@ android:id/tabhost"没有* (2认同)

sil*_*arf 7

Project > Clean...如果您使用的是Eclipse,请尝试从菜单中清除构建.听起来很简单,但经常解决这个问题.

  • 将`setContentView(R.layout.main);`改为`setContentView(R.layout.visualizzaliste);`但我想你得到了! (7认同)
  • 我是一个非常大的白痴!非常感谢 !!!我把注意力集中在xml文件上,我没有看到这个......非常感谢你 (5认同)
  • 你的xml文件名为`main.xml`吗? (3认同)
  • 没有.它被称为visualizzaliste.xml(它是意大利语:)) (2认同)