为什么我从 TabWidget 收到空指针异常?

发布于 2024-09-03 09:33:09 字数 5224 浏览 10 评论 0原文

我正在编写一个 Android 程序,其中有一个使用选项卡的活动。

Activity

public class UnitActivity extends TabActivity {
  @Override
  public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);

  TabHost tabHost = getTabHost();
  TabSpec spec;
  Resources res = getResources();

  LayoutInflater.from(this).inflate(R.layout.unit_view, tabHost.getTabContentView(), true);

  spec = tabHost.newTabSpec("controls");
  spec.setIndicator("Control", res.getDrawable(R.drawable.ic_tab_equalizer));
  spec.setContent(R.id.txtview);
  tabHost.addTab(spec);
}

}

R.layout.unit_view 引用的 XML

<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/tabhost"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="5dp">
  <TabWidget android:id="@android:id/tabs"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"/>
    <FrameLayout android:id="@android:id/tabcontent"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:padding="5dp">
      <TextView android:id="@+id/txtview"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent"
          android:gravity="bottom"
          android:text="nullpointer this!" />
    </FrameLayout>
  </LinearLayout>
</TabHost>

据我所知,我正在做与 android sdk 的 tabs1 api 示例中看到的相同的事情。我尝试过“getLayoutInflator()”而不是“LayoutInflator.from(this)”,但结果相同。

如果我用“setContentView(R.layout.unit_view)”替换 LayoutInflater 行,我的程序不会因空指针异常而崩溃,但我的内容完全是空白的。我拿到了标签,就是这样。

我已经检查以确保 R.layout.unit_view 和 tabHost 在运行 LayoutInflater 行时不为 null,并且它们似乎没问题。它们肯定不是空的。我还检查过以确保 LayoutInflater.from(this) 返回一个有效的布局充气器对象,它确实返回了。

指示错误的 logcat 说

 E/AndroidRuntime(  541): java.lang.NullPointerException
 E/AndroidRuntime(  541):  at android.widget.TabWidget.dispatchDraw(TabWidget.java:206)
 E/AndroidRuntime(  541):  at android.view.ViewGroup.drawChild(ViewGroup.java:1529)
 E/AndroidRuntime(  541):  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1258)
 E/AndroidRuntime(  541):  at android.view.ViewGroup.drawChild(ViewGroup.java:1529)
 E/AndroidRuntime(  541):  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1258)
 E/AndroidRuntime(  541):  at android.view.ViewGroup.drawChild(ViewGroup.java:1529)
 E/AndroidRuntime(  541):  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1258)
 E/AndroidRuntime(  541):  at android.view.ViewGroup.drawChild(ViewGroup.java:1529)
 E/AndroidRuntime(  541):  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1258)
 E/AndroidRuntime(  541):  at android.view.ViewGroup.drawChild(ViewGroup.java:1529)
 E/AndroidRuntime(  541):  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1258)
 E/AndroidRuntime(  541):  at android.view.ViewGroup.drawChild(ViewGroup.java:1529)
 E/AndroidRuntime(  541):  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1258)
 E/AndroidRuntime(  541):  at android.view.View.draw(View.java:6538)
 E/AndroidRuntime(  541):  at android.widget.FrameLayout.draw(FrameLayout.java:352)
 E/AndroidRuntime(  541):  at android.view.ViewGroup.drawChild(ViewGroup.java:1531)
 E/AndroidRuntime(  541):  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1258)
 E/AndroidRuntime(  541):  at android.view.ViewGroup.drawChild(ViewGroup.java:1529)
 E/AndroidRuntime(  541):  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1258)
 E/AndroidRuntime(  541):  at android.view.View.draw(View.java:6538)
 E/AndroidRuntime(  541):  at android.widget.FrameLayout.draw(FrameLayout.java:352)
 E/AndroidRuntime(  541):  at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:1830)
 E/AndroidRuntime(  541):  at android.view.ViewRoot.draw(ViewRoot.java:1349)
 E/AndroidRuntime(  541):  at android.view.ViewRoot.performTraversals(ViewRoot.java:1114)
 E/AndroidRuntime(  541):  at android.view.ViewRoot.handleMessage(ViewRoot.java:1633)
 E/AndroidRuntime(  541):  at android.os.Handler.dispatchMessage(Handler.java:99)
 E/AndroidRuntime(  541):  at android.os.Looper.loop(Looper.java:123)
 E/AndroidRuntime(  541):  at android.app.ActivityThread.main(ActivityThread.java:4363)
 E/AndroidRuntime(  541):  at java.lang.reflect.Method.invokeNative(Native Method)
 E/AndroidRuntime(  541):  at java.lang.reflect.Method.invoke(Method.java:521)
 E/AndroidRuntime(  541):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
 E/AndroidRuntime(  541):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
 E/AndroidRuntime(  541):  at dalvik.system.NativeStart.main(Native Method)
 I/Process (   61): Sending signal. PID: 541 SIG: 3
 I/dalvikvm(  541): threadid=7: reacting to signal 3
 I/dalvikvm(  541): Wrote stack trace to '/data/anr/traces.txt'

任何人都知道如何将此内容放入选项卡而不会使我的应用程序崩溃?我的实际程序更复杂,并且有多个选项卡,但我将其简化为这样,试图找出它崩溃的原因,但它仍然崩溃,我不知道为什么。

如果我不使用 LayoutInflator,我的程序不会崩溃,但我也没有得到任何内容,只有选项卡。

I'm writing an android program in which I have an activity that uses tabs.

The Activity

public class UnitActivity extends TabActivity {
  @Override
  public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);

  TabHost tabHost = getTabHost();
  TabSpec spec;
  Resources res = getResources();

  LayoutInflater.from(this).inflate(R.layout.unit_view, tabHost.getTabContentView(), true);

  spec = tabHost.newTabSpec("controls");
  spec.setIndicator("Control", res.getDrawable(R.drawable.ic_tab_equalizer));
  spec.setContent(R.id.txtview);
  tabHost.addTab(spec);
}

}

The XML referenced by R.layout.unit_view

<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/tabhost"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="5dp">
  <TabWidget android:id="@android:id/tabs"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"/>
    <FrameLayout android:id="@android:id/tabcontent"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:padding="5dp">
      <TextView android:id="@+id/txtview"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent"
          android:gravity="bottom"
          android:text="nullpointer this!" />
    </FrameLayout>
  </LinearLayout>
</TabHost>

As far as I can see I'm doing the same thing I see in the tabs1 api sample from the android sdk. I've tried "getLayoutInflator()" instead of "LayoutInflator.from(this)" with the same result.

If I replace the LayoutInflater line with "setContentView(R.layout.unit_view)" my program doesn't crash with a null pointer exception but my content is completely blank and empty. I get the tab and that's it.

I've checked to make sure R.layout.unit_view and tabHost are not null when it runs the LayoutInflater line and they seem to be fine. They're defenitely not null. I've also checked to make sure LayoutInflater.from(this) returns a valid layout inflater object and it does.

The logcat indicating the error says

 E/AndroidRuntime(  541): java.lang.NullPointerException
 E/AndroidRuntime(  541):  at android.widget.TabWidget.dispatchDraw(TabWidget.java:206)
 E/AndroidRuntime(  541):  at android.view.ViewGroup.drawChild(ViewGroup.java:1529)
 E/AndroidRuntime(  541):  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1258)
 E/AndroidRuntime(  541):  at android.view.ViewGroup.drawChild(ViewGroup.java:1529)
 E/AndroidRuntime(  541):  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1258)
 E/AndroidRuntime(  541):  at android.view.ViewGroup.drawChild(ViewGroup.java:1529)
 E/AndroidRuntime(  541):  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1258)
 E/AndroidRuntime(  541):  at android.view.ViewGroup.drawChild(ViewGroup.java:1529)
 E/AndroidRuntime(  541):  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1258)
 E/AndroidRuntime(  541):  at android.view.ViewGroup.drawChild(ViewGroup.java:1529)
 E/AndroidRuntime(  541):  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1258)
 E/AndroidRuntime(  541):  at android.view.ViewGroup.drawChild(ViewGroup.java:1529)
 E/AndroidRuntime(  541):  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1258)
 E/AndroidRuntime(  541):  at android.view.View.draw(View.java:6538)
 E/AndroidRuntime(  541):  at android.widget.FrameLayout.draw(FrameLayout.java:352)
 E/AndroidRuntime(  541):  at android.view.ViewGroup.drawChild(ViewGroup.java:1531)
 E/AndroidRuntime(  541):  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1258)
 E/AndroidRuntime(  541):  at android.view.ViewGroup.drawChild(ViewGroup.java:1529)
 E/AndroidRuntime(  541):  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1258)
 E/AndroidRuntime(  541):  at android.view.View.draw(View.java:6538)
 E/AndroidRuntime(  541):  at android.widget.FrameLayout.draw(FrameLayout.java:352)
 E/AndroidRuntime(  541):  at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:1830)
 E/AndroidRuntime(  541):  at android.view.ViewRoot.draw(ViewRoot.java:1349)
 E/AndroidRuntime(  541):  at android.view.ViewRoot.performTraversals(ViewRoot.java:1114)
 E/AndroidRuntime(  541):  at android.view.ViewRoot.handleMessage(ViewRoot.java:1633)
 E/AndroidRuntime(  541):  at android.os.Handler.dispatchMessage(Handler.java:99)
 E/AndroidRuntime(  541):  at android.os.Looper.loop(Looper.java:123)
 E/AndroidRuntime(  541):  at android.app.ActivityThread.main(ActivityThread.java:4363)
 E/AndroidRuntime(  541):  at java.lang.reflect.Method.invokeNative(Native Method)
 E/AndroidRuntime(  541):  at java.lang.reflect.Method.invoke(Method.java:521)
 E/AndroidRuntime(  541):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
 E/AndroidRuntime(  541):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
 E/AndroidRuntime(  541):  at dalvik.system.NativeStart.main(Native Method)
 I/Process (   61): Sending signal. PID: 541 SIG: 3
 I/dalvikvm(  541): threadid=7: reacting to signal 3
 I/dalvikvm(  541): Wrote stack trace to '/data/anr/traces.txt'

Anybody have any idea how I can get this content into a tab without crashing my application? My actual program is more complex and has more than one tab but I simplified it down to this in an attempt to find out why it's crashing but it still crashes and I don't know why.

If I don't use LayoutInflator my program doesn't crash but I don't get any content either, just tabs.

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

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

发布评论

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

评论(4

压抑⊿情绪 2024-09-10 09:33:09

对于那些不在 TabActivity 内使用 TabHost 的人,请务必调用 TabHost.setup()。这是它的文档: http://developer.android.com /reference/android/widget/TabHost.html#setup()
当您尝试将选项卡添加到选项卡主机时,不这样做将导致 NullPointerException。

本教程没有提及这一点,所以我想我应该提请您注意它,以节省你们寻找它的时间。

For those of you who are using a TabHost not inside a TabActivity, then be sure to call TabHost.setup(). Here's the documentation for it: http://developer.android.com/reference/android/widget/TabHost.html#setup()
Not doing so will result in a NullPointerException when you try to add tabs to your tabhost.

The tutorial fails to mention that, so I thought I'd bring it to your attention to save you guys the time I spent looking for it.

再浓的妆也掩不了殇 2024-09-10 09:33:09

这对我有用:

tabHost = getTabHost();

        tabHost.addTab(tabHost.newTabSpec("upcoming").setIndicator(
                getResources().getString(R.string.upcoming)).setContent(R.id.cell1));

Partial XML

<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/tabhost" android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <LinearLayout android:orientation="vertical"
        android:layout_width="fill_parent" android:layout_height="fill_parent">
        <TabWidget android:id="@android:id/tabs"
            android:layout_width="fill_parent" android:layout_height="wrap_content" />
        <FrameLayout android:id="@android:id/tabcontent"
            android:layout_width="fill_parent" android:layout_height="fill_parent">
            <include android:id="@+id/cell1"
                layout="@layout/my_layout_file" />
             <!-- another include for cell2, and the closing tags -->

This does work for me:

tabHost = getTabHost();

        tabHost.addTab(tabHost.newTabSpec("upcoming").setIndicator(
                getResources().getString(R.string.upcoming)).setContent(R.id.cell1));

Partial XML

<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/tabhost" android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <LinearLayout android:orientation="vertical"
        android:layout_width="fill_parent" android:layout_height="fill_parent">
        <TabWidget android:id="@android:id/tabs"
            android:layout_width="fill_parent" android:layout_height="wrap_content" />
        <FrameLayout android:id="@android:id/tabcontent"
            android:layout_width="fill_parent" android:layout_height="fill_parent">
            <include android:id="@+id/cell1"
                layout="@layout/my_layout_file" />
             <!-- another include for cell2, and the closing tags -->
薄暮涼年 2024-09-10 09:33:09

只是想根据此处的陈述提供一些想法:

http:// code.google.com/p/android/issues/detail?id=14929

如果您尝试在加载数据之前绘制 TabWidget,它会导致崩溃。
为了防止这种情况,只需使用异步任务/处理程序不仅可以加载数据,还可以设置内容视图。
当然,您可以在数据加载过程中使用其他视图或进度对话框,只是暂时不要显示选项卡支架。

Just wanted to provide some idea based on the statements from here:

http://code.google.com/p/android/issues/detail?id=14929

The TabWidget is causing a crash if you try to draw it before loading the data.
To prevent this just use your async tasks/handlers not only to load the data but to set the content view too.
Of course you could use some other view or progress dialog during the data loading process, just don't present the tabs holder yet.

风为裳 2024-09-10 09:33:09

年!
您需要

调用: TabHost.setup()

在调用该方法之前 。
如果不是,TabHost 的 TabWidget 应该为 null。
我犯这个错误已经很久了。

YEAR!
You need to call:

TabHost.setup()

before call the method.
if not,the TabHost's TabWidget should be null.
I made this mistake for so long.

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