2011-12-16 54 views
7

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

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); 
} 

} 

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> 

and 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) 

问题

有人能告诉我为什么它说

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

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

+0

您是否尝试过重建整个项目?有时候eclipse在这些情况下有点buggy – poitroae 2011-12-16 18:23:21

+0

尝试删除你的R文件并重建。 – coder 2011-12-16 18:25:54

+0

我尝试清理该项目,重新启动它,并删除R但没有任何内容。我试图在另一个应用程序中使用此代码,它已启动,但在此不运行 – 2011-12-16 18:33:18

回答

7

如果您使用的是Eclipse,请尝试从Project > Clean...菜单清理您的版本。听起来很简单,但通常可以解决这个问题。

19

相信消息含义:

 <TabHost android:id="@+id/tabhost" 

应改为:

 <TabHost android:id="@android:id/tabhost" 
2

你只需要改变的事情就是Android:布局XML id属性,它应该是“机器人:id =“@ android:id/tabhost”“

1

我有同样的问题。 其实我扩展TabActivityDaAcquistareActivity类。这是原因。我通过将DaAcquistareActivityActivity而不是TabActivity解决了问题。

0

解决方案。 如果您切换到新的活动检查它扩展,也许从主,复制反射,并有一个TabActivity,但我们需要活动或其他活动。

相关问题