帮助解决 API 级别 5 上的 NullPointerException

发布于 2024-10-08 14:33:00 字数 3168 浏览 6 评论 0原文

我使用 API 级别 8 开发了我的应用程序,但最低 SDK 为 3。我在运行 8 级别的模拟器和我的 DroidX 以及一些朋友的手机上进行了大部分测试。到目前为止没有问题。还应该知道,我没有使用任何 3 级以上的 API,并且选项卡布局几乎是 Google 提供的示例的精确副本 此处。所以......当在具有 5 级 API 的模拟器上运行时,我收到以下错误,除了对 TabWidget 的引用之外,我不知道它来自哪里。

Uncaught handler: thread main exiting due to uncaught exception
java.lang.NullPointerException
  at android.widget.TabWidget.dispatchDraw(TabWidget.java:206)
  at android.view.ViewGroup.drawChild(ViewGroup.java:1524)
  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1256)
  at android.view.ViewGroup.drawChild(ViewGroup.java:1524)
  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1256)
  at android.view.ViewGroup.drawChild(ViewGroup.java:1524)
  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1256)
  at android.view.View.draw(View.java:6539)
  at android.widget.FrameLayout.draw(FrameLayout.java:352)
  at android.view.ViewGroup.drawChild(ViewGroup.java:1526)
  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1256)
  at android.view.View.draw(View.java:6539)
  at android.widget.FrameLayout.draw(FrameLayout.java:352)
  at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:1830)
  at android.view.ViewRoot.draw(ViewRoot.java:1348)
  at android.view.ViewRoot.performTraversals(ViewRoot.java:1113)
  at android.view.ViewRoot.handleMessage(ViewRoot.java:1632)
  at android.os.Handler.dispatchMessage(Handler.java:99)
  at android.os.Looper.loop(Looper.java:123)
  at android.app.ActivityThread.main(ActivityThread.java:4310)
      at java.lang.reflect.Method.invokeNative(Native Method)
  at java.lang.reflect.Method.invoke(Method.java:521)
  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
  at dalvik.system.NativeStart.main(Native Method)


    Resources res = getResources(); // Resource object to get Drawables
    TabHost tabHost = getTabHost();  // The activity TabHost
    TabHost.TabSpec spec;  // Resusable TabSpec for each tab
    Intent intent;  // Reusable Intent for each tab

    // Create an Intent to launch an Activity for the tab (to be reused)
    intent = new Intent().setClass(this, parent.class);

    // Initialize a TabSpec for each tab and add it to the TabHost
    spec = tabHost.newTabSpec("browse").setIndicator("Browse",
                      res.getDrawable(R.drawable.ic_tab_browse))
                  .setContent(intent);
    tabHost.addTab(spec);

    intent = new Intent().setClass(this, bookmarks.class);
    spec = tabHost.newTabSpec("bookmarks").setIndicator("Bookmarks",
                      res.getDrawable(R.drawable.ic_tab_bookmarks))
                  .setContent(intent);
    tabHost.addTab(spec);

    // Do the same for the other tabs
    intent = new Intent().setClass(this, search.class);
    spec = tabHost.newTabSpec("search").setIndicator("Search",
                      res.getDrawable(R.drawable.ic_tab_search))
                  .setContent(intent);
    tabHost.addTab(spec);
    tabHost.setCurrentTab(0);

I developed my application using API level 8 but the minimum SDK is 3. I did most of the testing on an emulator running 8 and my DroidX as well as some friends phones. No problems thus far. It should also be known that I did not use any API's above Level 3, and the Tab Layout was almost and exact copy of the example provided by Google here. So.... when running on an emulator with level 5 API I get the following error and I have no clue where it's coming from except for the references to TabWidget.

Uncaught handler: thread main exiting due to uncaught exception
java.lang.NullPointerException
  at android.widget.TabWidget.dispatchDraw(TabWidget.java:206)
  at android.view.ViewGroup.drawChild(ViewGroup.java:1524)
  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1256)
  at android.view.ViewGroup.drawChild(ViewGroup.java:1524)
  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1256)
  at android.view.ViewGroup.drawChild(ViewGroup.java:1524)
  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1256)
  at android.view.View.draw(View.java:6539)
  at android.widget.FrameLayout.draw(FrameLayout.java:352)
  at android.view.ViewGroup.drawChild(ViewGroup.java:1526)
  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1256)
  at android.view.View.draw(View.java:6539)
  at android.widget.FrameLayout.draw(FrameLayout.java:352)
  at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:1830)
  at android.view.ViewRoot.draw(ViewRoot.java:1348)
  at android.view.ViewRoot.performTraversals(ViewRoot.java:1113)
  at android.view.ViewRoot.handleMessage(ViewRoot.java:1632)
  at android.os.Handler.dispatchMessage(Handler.java:99)
  at android.os.Looper.loop(Looper.java:123)
  at android.app.ActivityThread.main(ActivityThread.java:4310)
      at java.lang.reflect.Method.invokeNative(Native Method)
  at java.lang.reflect.Method.invoke(Method.java:521)
  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
  at dalvik.system.NativeStart.main(Native Method)


    Resources res = getResources(); // Resource object to get Drawables
    TabHost tabHost = getTabHost();  // The activity TabHost
    TabHost.TabSpec spec;  // Resusable TabSpec for each tab
    Intent intent;  // Reusable Intent for each tab

    // Create an Intent to launch an Activity for the tab (to be reused)
    intent = new Intent().setClass(this, parent.class);

    // Initialize a TabSpec for each tab and add it to the TabHost
    spec = tabHost.newTabSpec("browse").setIndicator("Browse",
                      res.getDrawable(R.drawable.ic_tab_browse))
                  .setContent(intent);
    tabHost.addTab(spec);

    intent = new Intent().setClass(this, bookmarks.class);
    spec = tabHost.newTabSpec("bookmarks").setIndicator("Bookmarks",
                      res.getDrawable(R.drawable.ic_tab_bookmarks))
                  .setContent(intent);
    tabHost.addTab(spec);

    // Do the same for the other tabs
    intent = new Intent().setClass(this, search.class);
    spec = tabHost.newTabSpec("search").setIndicator("Search",
                      res.getDrawable(R.drawable.ic_tab_search))
                  .setContent(intent);
    tabHost.addTab(spec);
    tabHost.setCurrentTab(0);

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

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

发布评论

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

评论(2

夜清冷一曲。 2024-10-15 14:33:01

由于没有可使用的代码,很难给您太多建议。

  • 如果您不使用 TabActivity,请确保在使用 TabHost 执行任何其他操作之前调用 setup()。 p>

  • 由于异常来自 TabWidget,请尝试暂时简化选项卡指示器,看看这是否有助于本地化或解决问题。

With no code to work with, it is difficult to give you much advice.

  • If you are not using TabActivity, make sure you call setup() on your TabHost before doing anything else with it.

  • Since the exception is coming from TabWidget, try temporarily simplifying your tab indicators and see if that helps localize or clear up the problem.

稚然 2024-10-15 14:33:00

当我将其保存在 LinearLayout 中时,我的 tabhost 显示了 2.1 更新的 nullPointer 异常,但是当我尝试相对布局时,它工作正常。
所以如果你陷入同样的​​情况,这个改变可能会对你有所帮助。

My tabhost was showing nullPointer exception for 2.1 updated at the time when i kept it in linearLayout,but when i tried Relative layout it workes fine .
so might be this change help u if stucking in same situation .

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