为什么我从 TabWidget 得到空指针异常? [英] Why do I get a null pointer exception from TabWidget?

查看:23
本文介绍了为什么我从 TabWidget 得到空指针异常?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

活动

public class UnitActivity extends TabActivity {@覆盖public void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);TabHost tabHost = getTabHost();TabSpec 规范;资源 res = getResources();LayoutInflater.from(this).inflate(R.layout.unit_view, tabHost.getTabContentView(), true);spec = tabHost.newTabSpec("控件");spec.setIndicator("Control", res.getDrawable(R.drawable.ic_tab_equalizer));spec.setContent(R.id.txtview);tabHost.addTab(spec);}

}

R.layout.unit_view 引用的 XML

<线性布局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:text="空指针这个!"/></FrameLayout></LinearLayout></TabHost>

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

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

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

指示错误的 logcat 说

<前>E/AndroidRuntime(541):java.lang.NullPointerExceptionE/AndroidRuntime(541): 在 android.widget.TabWidget.dispatchDraw(TabWidget.java:206)E/AndroidRuntime(541):在android.view.ViewGroup.drawChild(ViewGroup.java:1529)E/AndroidRuntime(541): 在 android.view.ViewGroup.dispatchDraw(ViewGroup.java:1258)E/AndroidRuntime(541):在android.view.ViewGroup.drawChild(ViewGroup.java:1529)E/AndroidRuntime(541): 在 android.view.ViewGroup.dispatchDraw(ViewGroup.java:1258)E/AndroidRuntime(541):在android.view.ViewGroup.drawChild(ViewGroup.java:1529)E/AndroidRuntime(541): 在 android.view.ViewGroup.dispatchDraw(ViewGroup.java:1258)E/AndroidRuntime(541):在android.view.ViewGroup.drawChild(ViewGroup.java:1529)E/AndroidRuntime(541): 在 android.view.ViewGroup.dispatchDraw(ViewGroup.java:1258)E/AndroidRuntime(541):在android.view.ViewGroup.drawChild(ViewGroup.java:1529)E/AndroidRuntime(541): 在 android.view.ViewGroup.dispatchDraw(ViewGroup.java:1258)E/AndroidRuntime(541):在android.view.ViewGroup.drawChild(ViewGroup.java:1529)E/AndroidRuntime(541): 在 android.view.ViewGroup.dispatchDraw(ViewGroup.java:1258)E/AndroidRuntime(541): 在 android.view.View.draw(View.java:6538)E/AndroidRuntime(541): 在 android.widget.FrameLayout.draw(FrameLayout.java:352)E/AndroidRuntime(541): 在 android.view.ViewGroup.drawChild(ViewGroup.java:1531)E/AndroidRuntime(541): 在 android.view.ViewGroup.dispatchDraw(ViewGroup.java:1258)E/AndroidRuntime(541):在android.view.ViewGroup.drawChild(ViewGroup.java:1529)E/AndroidRuntime(541): 在 android.view.ViewGroup.dispatchDraw(ViewGroup.java:1258)E/AndroidRuntime(541): 在 android.view.View.draw(View.java:6538)E/AndroidRuntime(541): 在 android.widget.FrameLayout.draw(FrameLayout.java:352)E/AndroidRuntime(541):在 com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:1830)E/AndroidRuntime(541):在android.view.ViewRoot.draw(ViewRoot.java:1349)E/AndroidRuntime(541): 在 android.view.ViewRoot.performTraversals(ViewRoot.java:1114)E/AndroidRuntime(541):在android.view.ViewRoot.handleMessage(ViewRoot.java:1633)E/AndroidRuntime(541):在 android.os.Handler.dispatchMessage(Handler.java:99)E/AndroidRuntime(541): 在 android.os.Looper.loop(Looper.java:123)E/AndroidRuntime(541):在android.app.ActivityThread.main(ActivityThread.java:4363)E/AndroidRuntime(541): 在 java.lang.reflect.Method.invokeNative(Native Method)E/AndroidRuntime(541): 在 java.lang.reflect.Method.invoke(Method.java:521)E/AndroidRuntime(541): 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)E/AndroidRuntime(541): 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)E/AndroidRuntime(541): 在 dalvik.system.NativeStart.main(Native Method)I/Process (61):发送信号.PID:541 SIG:3I/dalvikvm(541):threadid=7:对信号 3 做出反应I/dalvikvm(541):将堆栈跟踪写入/data/anr/traces.txt"

有人知道如何在不使应用程序崩溃的情况下将这些内容放入选项卡吗?我的实际程序更复杂,有多个选项卡,但我将其简化为这样,试图找出它崩溃的原因,但它仍然崩溃,我不知道为什么.

如果我不使用 LayoutInflator,我的程序不会崩溃,但我也没有得到任何内容,只有标签.

解决方案

这对我有用:

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

部分 XML

<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"><包括 android:id="@+id/cell1"layout="@layout/my_layout_file"/><!-- cell2 的另一个包含,以及结束标签 -->

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.

解决方案

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 -->

这篇关于为什么我从 TabWidget 得到空指针异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆