1

在寻找解决方案后,我决定重新提出这里已经提出的问题。

我有一个 TabHost 控件,我需要将它放在其他布局(LinearLayout)中。我收集了谷歌给我们的例子(http://developer.android.com/resources/tutorials/views/hello-tabwidget.html),并且做了同样的事情,唯一的区别是 TabHost 不是布局的根,并且显示视图的activity类没有扩展TabActivity,而是Activity

我的xml是这样的:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <TabHost android:id="@+id/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="53px"
                android:tabStripEnabled="false" android:layout_margin="1px"
                android:background="#222222" />
            <FrameLayout android:id="@android:id/tabcontent"
                android:layout_width="fill_parent" android:layout_height="fill_parent"
                android:padding="5dp" />
        </LinearLayout>
    </TabHost>
</LinearLayout>

java类是这样的:

public class TabsExampleActivity extends Activity {

    TabHost tabHost;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        initTabs();

    }


    private void initTabs() {
        Resources res = getResources();                     // Resource object to get Drawables
        tabHost = (TabHost) findViewById(R.id.tabhost);     // The activity TabHost

        TabHost.TabSpec spec;                               // Resusable TabSpec for each tab
        Intent intent;                                      // Reusable Intent for each tab        

        intent = new Intent("eu.pkg.tabsExample.BuildingTabActivity");
        spec = tabHost.newTabSpec("text").setIndicator("",
                          res.getDrawable(R.drawable.tab_text_selector))
                      .setContent(intent);
        tabHost.addTab(spec);

        intent = new Intent().setClass(this, ClipArtTabActivity.class);
        spec = tabHost.newTabSpec("clipart").setIndicator("",
                          res.getDrawable(R.drawable.tab_clipart_selector))
                      .setContent(intent);
        tabHost.addTab(spec);


        intent = new Intent().setClass(this, FrameSelectorTabActivity.class);
        spec = tabHost.newTabSpec("frame").setIndicator("",
                          res.getDrawable(R.drawable.tab_frame_selector))
                      .setContent(intent);
        tabHost.addTab(spec);



        intent = new Intent().setClass(this, BgSelectorTabActivity.class);
        spec = tabHost.newTabSpec("bg").setIndicator("",
                          res.getDrawable(R.drawable.tab_bg_selector))
                      .setContent(intent);
        tabHost.addTab(spec);

        intent = new Intent().setClass(this, BuildingTabActivity.class);
        spec = tabHost.newTabSpec("build").setIndicator("",
                          res.getDrawable(R.drawable.tab_build_selector))
                      .setContent(intent);
        tabHost.addTab(spec);

        tabHost.setCurrentTab(4);
    }
}

如果我将 TabActivity 用作基本活动,并将 TabHost 用作 root,那么代码运行良好,我看到了选项卡......等等。

仅当我扩展 Activity 并且错误是:

04-11 18:53:38.874: 错误/AndroidRuntime(32539): java.lang.RuntimeException: 无法启动活动 ComponentInfo{eu.pkg.tabsExample/eu.pkg.tabsExample.TabsExampleActivity}: java.lang.NullPointerException

问题是:如何在 LinearLayout 中托管 TabHost

编辑:

按照评论中的建议进行更改后,日志猫错误为:

04-11 20:52:01.993: 错误/imdg81(28747): IsShutDownStarted() 04-11 20:52:01.999: 错误/SensorManager(28747): registerListener 4:gp2a 光传感器延迟:240 04-11 20:52 :01.999:错误/SensorManager(28747):=======>>>传感器线程正在运行<<<======== 04-11 20:52:02.075:错误/imdg81(28747): IsShutDownStarted() 04-11 20:52:02.284:错误/LockPatternKeyguardView(28747):电话已启动完成。所以可以为音乐播放器发送广播消息 04-11 20:52:05.507: ERROR/jdwp(3675): 写入握手字节失败: 管道损坏 (-1 of 14) 04-11 20:52:05.609: ERROR/AndroidRuntime( 3675):致命异常:主要 04-11 20:52:05.609:错误/AndroidRuntime(3675):java.lang.RuntimeException:无法启动活动 ComponentInfo{eu.pkg.tabsExample/eu.pkg.tabsExample.TabsExampleActivity}: java.lang.IllegalStateException:您是否忘记调用“public void setup(LocalActivityManager activityGroup)”?04-11 20:52:05.609: 错误/AndroidRuntime(3675): 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663) 04-11 20:52:05.609: 错误/AndroidRuntime(3675): 在 android. app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679) 04-11 20:52:05.609: 错误/AndroidRuntime(3675): 在 android.app.ActivityThread.access$2300(ActivityThread.java:125) 04-11 20:52 :05.609: ERROR/AndroidRuntime(3675): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033) 04-11 20:52:05.609: ERROR/AndroidRuntime(3675): at android.os.Handler。 dispatchMessage(Handler.java:99) 04-11 20:52:05.609: ERROR/AndroidRuntime(3675): at android.os.Looper.loop(Looper.java:123) 04-11 20:52:05.609: 公共无效设置(LocalActivityManager 活动组)'?04-11 20:52:05.609: 错误/AndroidRuntime(3675): 在 android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:654) 04-11 20:52:05.609: 错误/AndroidRuntime(3675): 在android.widget.TabHost.setCurrentTab(TabHost.java:328) 04-11 20:52:05.609: 错误/AndroidRuntime(3675): 在 android.widget.TabHost.addTab(TabHost.java:213) 04-11 20: 52:05.609: ERROR/AndroidRuntime(3675): at eu.pkg.tabsExample.TabsExampleActivity.initTabs(TabsExampleActivity.java:43) 04-11 20:52:05.609: ERROR/AndroidRuntime(3675): at eu.pkg.tabsExample .TabsExampleActivity.onCreate(TabsExampleActivity.java:27) 04-11 20:52:05.609: 错误/AndroidRuntime(3675): 在 android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 04-11 20:52:05.609 :

4

4 回答 4

6

问题解决了。正如用户 207 在其中一个评论中给出的,来自另一个 stackoverflow 问题的正确解决方案是:

这是 XML:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <TabHost android:id="@android:id/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="53px"
                android:tabStripEnabled="false" android:layout_margin="1px"
                android:background="#222222" />
            <FrameLayout android:id="@android:id/tabcontent"
                android:layout_width="fill_parent" android:layout_height="fill_parent"
                android:padding="5dp" />
        </LinearLayout>
    </TabHost>
</LinearLayout>

和java文件:

package eu.pkg.tabsExample;

import android.app.Activity;
import android.app.LocalActivityManager;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.os.Handler;
import android.widget.TabHost;

public class TabsExampleActivity extends Activity {

    TabHost tabHost;
    LocalActivityManager mLocalActivityManager;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        initTabs(savedInstanceState);
    }

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

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

    private void initTabs(Bundle savedInstanceState) {
        Resources res = getResources();                     // Resource object to get Drawables
        tabHost = (TabHost) findViewById(android.R.id.tabhost);     // The activity TabHost
        mLocalActivityManager = new LocalActivityManager(this, false);
        mLocalActivityManager.dispatchCreate(savedInstanceState);
        tabHost.setup(mLocalActivityManager);

        TabHost.TabSpec spec;                               // Resusable TabSpec for each tab
        Intent intent;                                      // Reusable Intent for each tab        

        intent = new Intent("eu.pkg.tabsExample.BuildingTabActivity");
        spec = tabHost.newTabSpec("text").setIndicator("",
                          res.getDrawable(R.drawable.tab_text_selector))
                      .setContent(intent);
        tabHost.addTab(spec);

        intent = new Intent().setClass(this, ClipArtTabActivity.class);
        spec = tabHost.newTabSpec("clipart").setIndicator("",
                          res.getDrawable(R.drawable.tab_clipart_selector))
                      .setContent(intent);
        tabHost.addTab(spec);


        intent = new Intent().setClass(this, FrameSelectorTabActivity.class);
        spec = tabHost.newTabSpec("frame").setIndicator("",
                          res.getDrawable(R.drawable.tab_frame_selector))
                      .setContent(intent);
        tabHost.addTab(spec);



        intent = new Intent().setClass(this, BgSelectorTabActivity.class);
        spec = tabHost.newTabSpec("bg").setIndicator("",
                          res.getDrawable(R.drawable.tab_bg_selector))
                      .setContent(intent);
        tabHost.addTab(spec);

        intent = new Intent().setClass(this, BuildingTabActivity.class);
        spec = tabHost.newTabSpec("build").setIndicator("",
                          res.getDrawable(R.drawable.tab_build_selector))
                      .setContent(intent);
        tabHost.addTab(spec);

        tabHost.setCurrentTab(4);
    }
}

幕后的魔力在于这 4 行代码:

    tabHost = (TabHost) findViewById(android.R.id.tabhost);     // The activity TabHost
    mLocalActivityManager = new LocalActivityManager(this, false);
    mLocalActivityManager.dispatchCreate(savedInstanceState);
    tabHost.setup(mLocalActivityManager);

感谢大家对此答案的贡献。

于 2012-04-12T14:40:44.553 回答
0

将解决您的错误。

另外:我刚刚读到您想要扩展Activity而不是TabActivity因为您正在尝试实现“嵌套选项卡”。在TabActivity另一个TabActivity. 以前没有这样做过,但我没有看到任何可能出现的问题。Activity添加或TabActivity作为选项卡内容没有区别。你可以试一试

于 2012-04-12T09:11:12.010 回答
0

更改此特定行

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

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

编辑

在以下行之后添加

tabHost = (TabHost) findViewById(R.id.tabhost);
tabHost.setup();
于 2012-04-11T17:13:03.517 回答
0

如果您使用 Activity 而不是扩展 TabActivity,这是解决方案

https://stackoverflow.com/a/5098774/563306

于 2012-04-11T17:01:00.637 回答