TabActivity 启动时的 NPE

发布于 2024-10-21 15:58:10 字数 2347 浏览 0 评论 0原文

我有一个由 2 个选项卡组成的活动。问题是,当我有以下代码时:

tabHost.setCurrentTab(0);

我收到以下异常:

03-14 01:06:39.520: ERROR/AndroidRuntime(588): java.lang.NullPointerException
03-14 01:06:39.520: ERROR/AndroidRuntime(588):     at android.widget.TabHost.dispatchWindowFocusChanged(TabHost.java:295)
03-14 01:06:39.520: ERROR/AndroidRuntime(588):     at android.view.ViewGroup.dispatchWindowFocusChanged(ViewGroup.java:661)
03-14 01:06:39.520: ERROR/AndroidRuntime(588):     at android.view.ViewGroup.dispatchWindowFocusChanged(ViewGroup.java:661)
03-14 01:06:39.520: ERROR/AndroidRuntime(588):     at android.view.ViewGroup.dispatchWindowFocusChanged(ViewGroup.java:661)
03-14 01:06:39.520: ERROR/AndroidRuntime(588):     at android.view.ViewGroup.dispatchWindowFocusChanged(ViewGroup.java:661)
03-14 01:06:39.520: ERROR/AndroidRuntime(588):     at android.widget.TabHost.dispatchWindowFocusChanged(TabHost.java:295)
03-14 01:06:39.520: ERROR/AndroidRuntime(588):     at android.view.ViewGroup.dispatchWindowFocusChanged(ViewGroup.java:661)
03-14 01:06:39.520: ERROR/AndroidRuntime(588):     at android.view.ViewGroup.dispatchWindowFocusChanged(ViewGroup.java:661)
03-14 01:06:39.520: ERROR/AndroidRuntime(588):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1819)
03-14 01:06:39.520: ERROR/AndroidRuntime(588):     at android.os.Handler.dispatchMessage(Handler.java:99)
03-14 01:06:39.520: ERROR/AndroidRuntime(588):     at android.os.Looper.loop(Looper.java:123)
03-14 01:06:39.520: ERROR/AndroidRuntime(588):     at android.app.ActivityThread.main(ActivityThread.java:4363)
03-14 01:06:39.520: ERROR/AndroidRuntime(588):     at java.lang.reflect.Method.invokeNative(Native Method)
03-14 01:06:39.520: ERROR/AndroidRuntime(588):     at java.lang.reflect.Method.invoke(Method.java:521)
03-14 01:06:39.520: ERROR/AndroidRuntime(588):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
03-14 01:06:39.520: ERROR/AndroidRuntime(588):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
03-14 01:06:39.520: ERROR/AndroidRuntime(588):     at dalvik.system.NativeStart.main(Native Method)

但是,如果我更改默认显示的第二个选项卡(我只有 2 个选项卡) ),如:

tabHost.setCurrentTab(1);

一切都正确显示并且我也可以在选项卡之间切换。这告诉我选项卡以某种方式配置正确,但是为什么会出现这种行为?

I have an activity that is formed of 2 tabs. The problem is, when I have this code:

tabHost.setCurrentTab(0);

I am getting the following exception:

03-14 01:06:39.520: ERROR/AndroidRuntime(588): java.lang.NullPointerException
03-14 01:06:39.520: ERROR/AndroidRuntime(588):     at android.widget.TabHost.dispatchWindowFocusChanged(TabHost.java:295)
03-14 01:06:39.520: ERROR/AndroidRuntime(588):     at android.view.ViewGroup.dispatchWindowFocusChanged(ViewGroup.java:661)
03-14 01:06:39.520: ERROR/AndroidRuntime(588):     at android.view.ViewGroup.dispatchWindowFocusChanged(ViewGroup.java:661)
03-14 01:06:39.520: ERROR/AndroidRuntime(588):     at android.view.ViewGroup.dispatchWindowFocusChanged(ViewGroup.java:661)
03-14 01:06:39.520: ERROR/AndroidRuntime(588):     at android.view.ViewGroup.dispatchWindowFocusChanged(ViewGroup.java:661)
03-14 01:06:39.520: ERROR/AndroidRuntime(588):     at android.widget.TabHost.dispatchWindowFocusChanged(TabHost.java:295)
03-14 01:06:39.520: ERROR/AndroidRuntime(588):     at android.view.ViewGroup.dispatchWindowFocusChanged(ViewGroup.java:661)
03-14 01:06:39.520: ERROR/AndroidRuntime(588):     at android.view.ViewGroup.dispatchWindowFocusChanged(ViewGroup.java:661)
03-14 01:06:39.520: ERROR/AndroidRuntime(588):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1819)
03-14 01:06:39.520: ERROR/AndroidRuntime(588):     at android.os.Handler.dispatchMessage(Handler.java:99)
03-14 01:06:39.520: ERROR/AndroidRuntime(588):     at android.os.Looper.loop(Looper.java:123)
03-14 01:06:39.520: ERROR/AndroidRuntime(588):     at android.app.ActivityThread.main(ActivityThread.java:4363)
03-14 01:06:39.520: ERROR/AndroidRuntime(588):     at java.lang.reflect.Method.invokeNative(Native Method)
03-14 01:06:39.520: ERROR/AndroidRuntime(588):     at java.lang.reflect.Method.invoke(Method.java:521)
03-14 01:06:39.520: ERROR/AndroidRuntime(588):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
03-14 01:06:39.520: ERROR/AndroidRuntime(588):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
03-14 01:06:39.520: ERROR/AndroidRuntime(588):     at dalvik.system.NativeStart.main(Native Method)

However, if I change the second tab to be displayed by default (I have only 2 tabs), as in:

tabHost.setCurrentTab(1);

everything is displayed correctly and I am able to switch between the tabs as well. This tells me that the tabs are configured correctly somehow, but then, why this behavior ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

白云不回头 2024-10-28 15:58:10

我意识到我做错了什么:Tab 机制工作正常,是为第一个选项卡实现的 ListActivity 导致了 NPE。

I realized what I was doing wrong: the Tab mechanism was working ok, it was a ListActivity implemented for the first tab that was causing the NPE.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文