Android:没有 TabActivity 的 TabHost [英] Android: TabHost without TabActivity
本文介绍了Android:没有 TabActivity 的 TabHost的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想创建标签而不扩展 TabActivity.(原因是 TabActivity 似乎无法处理自定义标题栏).我有
I want to create tabs without extending TabActivity. (The reason is that TabActivity cannot handle a custom titlebar as it seems). I have
public class startTab extends Activity {
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.mylayout);
Resources res = getResources();
LocalActivityManager mlam = new LocalActivityManager(this, false);
TabHost tabHost = (TabHost) findViewById(R.id.tabhost);
tabHost.setup(mlam);
TabHost.TabSpec spec;
Intent intent;
intent = new Intent().setClass(this, Show1.class);
spec = tabHost.newTabSpec("Items").setIndicator("Items", res.getDrawable(R.drawable.items32_ldpi)).setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this, Show2.class);
spec = tabHost.newTabSpec("Users").setIndicator("Users",res.getDrawable(R.drawable.user32_ldpi)).setContent(intent);
tabHost.addTab(spec);
}
}
我得到的错误是
07-02 07:11:12.715: ERROR/AndroidRuntime(411):
Caused by: java.lang.IllegalStateException: Activities can't be added until the containing group has been created.
视图的xml是
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tabhost" android:orientation="vertical" 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:paddingTop="5dip">
<TabWidget android:id="@android:id/tabs"
android:layout_width="fill_parent" android:layout_height="fill_parent"></TabWidget>
<FrameLayout android:id="@android:id/tabcontent"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:paddingTop="5dip">
</FrameLayout>
</LinearLayout>
</TabHost>
我在某处读到我必须使用 LocalActivityManager,我认为我在那里遗漏了一些东西.有人有想法吗?
I read somewhere that I have to use a LocalActivityManager, I assume that I am missing something there. Anyone an idea?
谢谢!
推荐答案
之前调用 tabHost.setup(mLocalActivityManager);你需要添加这一行.
Before calling tabHost.setup(mLocalActivityManager); you need to add this line.
mlam.dispatchCreate(savedInstanceState);
tabHost.setup(mlam );
同样的,你需要为onResume添加,
similarly, you need to add for onResume,
mlam.dispatchResume();
onPause(),
mlam.dispatchPause(isFinishing());
这篇关于Android:没有 TabActivity 的 TabHost的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文