Android:TabSpec setContent(View) 中的 NPE
我试图将 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 文件:
<include layout="@layout/lobby_tab"/>
这解决了问题。感谢您抽出时间!
I'm trying to set the content of a tab in my TabHost to be a RelativeLayout (defined in the top-level of one of my XML files).
I've tried identifying it both by using R.layout.lobby_tab and R.id.lobby_base (the ID is added as an item in XML declaration).
Either way, I get a NullPointerException in the setContent(View) method:
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
Here is the code that runs to set up the tabs:
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);
}
UPDATE (Issue Resolved)
I needed to add this line to the FrameLayout in the XML file:
<include layout="@layout/lobby_tab"/>
This resolved the issue. Thanks for your time!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当您不从 TabActivity 扩展 Activity 时,也会出现此错误 - 在这种情况下,您需要手动调用
TabHost
实例上的setup()
方法。This bug also manifests when you do not extend your activity from TabActivity - in this case you need to manually call the
setup()
method on theTabHost
instance.