为什么我会从TabWidget一个空指针异常?我会、指针、异常、TabWidget

2023-09-06 22:32:11 作者:拿命疼你

我正在写一个Android程序中,我有一个使用标签的活动。

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

该活动

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

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>

据我可以看到我在做同样的事情,我从Android SDK中的TABS1 API样品中看到的。我已经试过(这)LayoutInflator.from的getLayoutInflator(),而不是具有相同的结果。

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.

如果我取代的setContentView(R.layout.unit_view)的LayoutInflater行我的程序不与一个空指针异常崩溃,但我的内容是完全空白和空白。我得到的标签,仅此而已。

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.

我检查,以确保R.layout.unit_view和tabHost不为空时,它运行LayoutInflater线,他们似乎要被罚款。他们defenitely不为空。我也检查,以确保LayoutInflater.from(这)返回一个有效的布局充气物体和它的作用。

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.

该指示的logcat错误说:

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.

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

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

推荐答案

这不工作对我来说:

tabHost = getTabHost();

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

部分XML

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