我正在尝试将TabHost中的选项卡内容设置为RelativeLayout(在我的一个
XML文件的顶层中定义).
我已经尝试通过使用R.layout.lobby_tab和R.id.lobby_base来识别它(ID被添加为XML声明中的项目).
无论哪种方式,我在setContent(View)方法中得到一个NullPointerException:
02-12 22:42:12.907: E/AndroidRuntime(2028): FATAL EXCEPTION: main {webs.winbatch.dwd/webs.winbatch.dwd.HostActivity}: java.lang.NullPointerException 02-12 22:42:12.907: E/AndroidRuntime(2028): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647) 02-12 22:42:12.907: E/AndroidRuntime(2028): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663) 02-12 22:42:12.907: E/AndroidRuntime(2028): at android.app.ActivityThread.access$1500(ActivityThread.java:117) 02-12 22:42:12.907: E/AndroidRuntime(2028): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931) 02-12 22:42:12.907: E/AndroidRuntime(2028): at android.os.Handler.dispatchMessage(Handler.java:99) 02-12 22:42:12.907: E/AndroidRuntime(2028): at android.os.Looper.loop(Looper.java:123) 02-12 22:42:12.907: E/AndroidRuntime(2028): at android.app.ActivityThread.main(ActivityThread.java:3683) 02-12 22:42:12.907: E/AndroidRuntime(2028): at java.lang.reflect.Method.invokeNative(Native Method) 02-12 22:42:12.907: E/AndroidRuntime(2028): at java.lang.reflect.Method.invoke(Method.java:507) 02-12 22:42:12.907: E/AndroidRuntime(2028): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839) 02-12 22:42:12.907: E/AndroidRuntime(2028): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) 02-12 22:42:12.907: E/AndroidRuntime(2028): at dalvik.system.NativeStart.main(Native Method) 02-12 22:42:12.907: E/AndroidRuntime(2028): Caused by: java.lang.NullPointerException 02-12 22:42:12.907: E/AndroidRuntime(2028): at android.widget.TabHost$ViewIdContentStrategy.<init>(TabHost.java:589) 02-12 22:42:12.907: E/AndroidRuntime(2028): at android.widget.TabHost$ViewIdContentStrategy.<init>(TabHost.java:584) 02-12 22:42:12.907: E/AndroidRuntime(2028): at android.widget.TabHost$TabSpec.setContent(TabHost.java:441) 02-12 22:42:12.907: E/AndroidRuntime(2028): at webs.winbatch.dwd.HostActivity.setUpTabs(HostActivity.java:20) 02-12 22:42:12.907: E/AndroidRuntime(2028): at webs.winbatch.dwd.HostActivity.onCreate(HostActivity.java:13) 02-12 22:42:12.907: E/AndroidRuntime(2028): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 02-12 22:42:12.907: E/AndroidRuntime(2028): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611) 02-12 22:42:12.907: E/AndroidRuntime(2028): ... 11 more
以下是运行以设置选项卡的代码:
TabHost thost = (TabHost) findViewById(android.R.id.tabhost); TabHost.TabSpec spec; if(thost !=null) { spec = thost.newTabSpec("lobby").setIndicator("Lobby").setContent(R.id.lobby_base); thost.addTab(spec); }
更新(已解决的问题)
<include layout="@layout/lobby_tab"/>
这解决了这个问题.谢谢你的时间!