Android:没有 TabActivity 的 TabHost

发布于 2024-09-08 13:55:43 字数 2086 浏览 3 评论 0原文

我想创建选项卡而不扩展 TabActivity。 (原因是 TabActivity 无法像看起来那样处理自定义标题栏)。我

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

}

The error I get is

    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.

The xml for the view is

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

I read somewhere that I have to use a LocalActivityManager, I assume that I am missing something there. Anyone an idea?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

停滞 2024-09-15 13:55:43

在调用 tabHost.setup(mLocalActivityManager); 之前你需要添加这一行。

mlam.dispatchCreate(savedInstanceState);
tabHost.setup(mlam );

同样,您需要添加 onResume、

mlam.dispatchResume(); 

onPause()、

 mlam.dispatchPause(isFinishing());

Before calling tabHost.setup(mLocalActivityManager); you need to add this line.

mlam.dispatchCreate(savedInstanceState);
tabHost.setup(mlam );

similarly, you need to add for onResume,

mlam.dispatchResume(); 

onPause(),

 mlam.dispatchPause(isFinishing());
债姬 2024-09-15 13:55:43

请考虑使用Views 作为选项卡的内容。这不仅会导致更少的代码、更少消耗的堆空间、更少消耗的堆栈空间和更低的 CPU 利用率,它还会帮助您解决这个问题。这里有两个 示例展示了这种技术。

Please consider using Views as the contents of your tabs. Not only will this result in less code, less consumed heap space, less consumed stack space, and lower CPU utilization, it will also get you past this problem. Here are two examples showing this technique.

风月客 2024-09-15 13:55:43
public class ScoreboardActivity extends Activity {
    LocalActivityManager mlam;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_scoreboard);
        mlam = new LocalActivityManager(this, false);
        mlam.dispatchCreate(savedInstanceState);
        TabHost th = (TabHost) findViewById(android.R.id.tabhost);
        th.setup(mlam);
        th.addTab(th.newTabSpec("Numpad").setIndicator("Numpad").setContent(R.id.tab1));
        th.addTab(th.newTabSpec("CardCount").setIndicator("CardCount").setContent(R.id.tab2));
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_scoreboard, menu);
        return true;


    }
    @Override
    protected void onResume(){
        super.onResume();
        mlam.dispatchResume();
    }

    @Override
    protected void onPause(){
        super.onPause();
        mlam.dispatchPause(isFinishing());
    }

}
public class ScoreboardActivity extends Activity {
    LocalActivityManager mlam;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_scoreboard);
        mlam = new LocalActivityManager(this, false);
        mlam.dispatchCreate(savedInstanceState);
        TabHost th = (TabHost) findViewById(android.R.id.tabhost);
        th.setup(mlam);
        th.addTab(th.newTabSpec("Numpad").setIndicator("Numpad").setContent(R.id.tab1));
        th.addTab(th.newTabSpec("CardCount").setIndicator("CardCount").setContent(R.id.tab2));
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_scoreboard, menu);
        return true;


    }
    @Override
    protected void onResume(){
        super.onResume();
        mlam.dispatchResume();
    }

    @Override
    protected void onPause(){
        super.onPause();
        mlam.dispatchPause(isFinishing());
    }

}
时光与爱终年不遇 2024-09-15 13:55:43

尽管有设计考虑,但以下代码根本不起作用,并且 API 似乎表明 setContent(Intent i) 有效。当 Activity 扩展 TabActivity 时,此方法有效,但是,扩展 Activity 并添加 setup() 调用会导致 android.widget 出现 异常.TabHost$IntentContentStrategy.getContentView(TabHost.java:649)

有趣的是,LogCat 表明我忘记调用 setup()

mTabHost = (TabHost) findViewById(android.R.id.tabhost);
mTabHost.setup();

Intent tab1Intent = new Intent(this, ActivityOne.class);
Button tab1View = new Button(this);
tab1View.setText("Activity 1");
  mTabHost.addTab(mTabHost.newTabSpec("tab_1").setIndicator(tab1View).setContent(tab1Intent));

Design considerations notwithstanding, the following does not work at all, and the API seems to indicate that setContent(Intent i) is valid. This works when the activity extends TabActivity, however, extending Activity and adding setup() call results in an exception at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:649)

Funny thing is, the LogCat suggests I forgot to call setup()

mTabHost = (TabHost) findViewById(android.R.id.tabhost);
mTabHost.setup();

Intent tab1Intent = new Intent(this, ActivityOne.class);
Button tab1View = new Button(this);
tab1View.setText("Activity 1");
  mTabHost.addTab(mTabHost.newTabSpec("tab_1").setIndicator(tab1View).setContent(tab1Intent));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文