您的内容必须有一个 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>
和 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)
问题
有人可以告诉我为什么这么说
您的内容必须有一个 id 属性为“android.R.id.tabhost”的 TabHost
当 android:id = @android:id/tabhost? 时,
I have a problem:
Java Code
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 code
<?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)
Problem
Someone can tell me why it's says
Your content must have a TabHost whose id attribute is 'android.R.id.tabhost'
when the android:id = @android:id/tabhost?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我相信这条消息的意思是:
应该改为:
I believe the message means this:
should be changed to:
尝试从
Project > 清理您的构建如果您使用的是 Eclipse,请清理...
菜单。听起来很简单,但通常可以解决这个问题。Try cleaning your build from the
Project > Clean...
menu if you are using Eclipse. Sounds simple but often fixes this problem.您唯一需要更改的是布局 xml 中的 android:id 属性,它应该是“ android:id="@android:id/tabhost" ”
Only thing you need to change is android:id attribute in layout xml , It should be " android:id="@android:id/tabhost" "
我也有同样的问题。
实际上,我为
DaAcquistareActivity
类扩展了TabActivity
。就是这个原因。我通过使用Activity
而不是使用TabActivity
扩展DaAcquistareActivity
解决了这个问题。I had the same problem.
Actually I extended
TabActivity
forDaAcquistareActivity
class. This was the reason. I solved the problem by extendingDaAcquistareActivity
withActivity
and not withTabActivity
.解决方案。
如果您要切换到新的 Activity,请检查它是否会扩展,可能是根据从主 Activity 复制的反射,并且有一个 TabActivity,但我们需要Activity 或其他 Activity。
Solution.
If you're switching to a new Activity check it extends, perhaps on reflex copied from the primary, and there is a TabActivity, but we need Activity or other activity.