Android:TabSpec中的NPE setContent(View)

我试图将我的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); } 

更新(已解决问题)

我需要将这一行添加到XML文件中的FrameLayout:

 <include layout="@layout/lobby_tab"/> 

这解决了这个问题。 谢谢你的时间!

Solutions Collecting From Web of "Android:TabSpec中的NPE setContent(View)"