在搜索解决方案后,我决定重新提出问题already asked here。
我有一个TabHost控件,我需要把它放在其他布局(LinearLayout)中。我收集谷歌给我们(http://developer.android.com/resources/tutorials/views/hello-tabwidget.html)的例子并做了同样的事情,唯一的区别是TabHost不是布局的根,显示视图的活动类不扩展TabActivity,但活动< / p>
我的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:ERROR / AndroidRuntime(32539): java.lang.RuntimeException:无法启动活动 ComponentInfo {eu.pkg.tabsExample / eu.pkg.tabsExample.TabsExampleActivity}: 显示java.lang.NullPointerException
问题是:如何在LinearLayout中托管TabHost ?
修改
按照评论中的建议进行更改后,日志cat错误为:
04-11 20:52:01.993:ERROR / imdg81(28747):IsShutDownStarted()04-11 20:52:01.999:ERROR / SensorManager(28747):registerListener 4:gp2a 光传感器延迟:240 04-11 20:52:01.999:ERROR / SensorManager(28747): =======&gt;&gt;&gt;传感器线程RUNNING&lt;&lt;&lt;&lt; ======== 04-11 20:52:02.075:ERROR / imdg81(28747):IsShutDownStarted()04 -11 20:52:02.284: ERROR / LockPatternKeyguardView(28747):电话已启动完成。所以可以 为音乐播放器发送广播消息04-11 20:52:05.507: ERROR / jdwp(3675):写入握手字节失败:管道损坏(-1) 14)04-11 20:52:05.609:ERROR / AndroidRuntime(3675):致命异常: main 04-11 20:52:05.609:ERROR / 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):at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663) 04-11 20:52:05.609:ERROR / AndroidRuntime(3675):at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679) 04-11 20:52:05.609:ERROR / AndroidRuntime(3675):at 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: 错误/ AndroidRuntime(3675):at android.app.ActivityThread.main(ActivityThread.java:4627)04-11 20:52:05.609:ERROR / AndroidRuntime(3675):at java.lang.reflect.Method.invokeNative(Native Method)04-11 20:52:05.609:ERROR / AndroidRuntime(3675):at java.lang.reflect.Method.invoke(Method.java:521)04-11 20:52:05.609: 错误/ AndroidRuntime(3675):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:858) 04-11 20:52:05.609:ERROR / AndroidRuntime(3675):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)04-11 20:52:05.609:ERROR / AndroidRuntime(3675):at dalvik.system.NativeStart.main(Native Method)04-11 20:52:05.609: 错误/ AndroidRuntime(3675):引起: java.lang.IllegalStateException:你忘了叫'public void setup(LocalActivityManager activityGroup)'? 04-11 20:52:05.609: 错误/ AndroidRuntime(3675):at android.widget.TabHost $ IntentContentStrategy.getContentView(TabHost.java:654) 04-11 20:52:05.609:ERROR / AndroidRuntime(3675):at android.widget.TabHost.setCurrentTab(TabHost.java:328)04-11 20:52:05.609:ERROR / AndroidRuntime(3675):at android.widget.TabHost.addTab(TabHost.java:213)04-11 20:52:05.609: 错误/ 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:ERROR / AndroidRuntime(3675):at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 04-11 20:52:05.609:ERROR / AndroidRuntime(3675):at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627) 04-11 20:52:05.609:ERROR / AndroidRuntime(3675):... 11更多
答案 0 :(得分:6)
问题解决了。 另一个stackoverflow问题中的用户207在其中一个评论中给出的正确解决方案是:
这是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);
}
}
场景背后的魔力在于这四行代码:
tabHost = (TabHost) findViewById(android.R.id.tabhost); // The activity TabHost
mLocalActivityManager = new LocalActivityManager(this, false);
mLocalActivityManager.dispatchCreate(savedInstanceState);
tabHost.setup(mLocalActivityManager);
谢谢大家对这个答案的贡献。
答案 1 :(得分:0)
如果您使用的是Activity而不是扩展TabActivity,那么这是解决方案
答案 2 :(得分:0)
更改此特定行
TabHost android:id =“@ + id / tabhost”
到
TabHost android:id =“@ android:id / tabhost”
修改强>
还在以下行之后添加
tabHost = (TabHost) findViewById(R.id.tabhost);
tabHost.setup();
答案 3 :(得分:0)
This将解决您的错误。
此外:我刚刚宣读您希望从Activity
而不是TabActivity
扩展,因为您正在尝试实现“嵌套标签”。在另一个TabActivity
内TabActivity
,这不是问题。之前没有这样做,但我没有看到任何可能出现的麻烦。添加Activity
或TabActivity
作为标签内容没有区别。你可以尝试一下