在寻找解决方案后,我决定重新提出这里已经提出的问题。
我有一个 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 :