2010-05-31 92 views
4

我正在写一个android程序,其中我有一个使用制表符的活动。为什么我会从TabWidget中获得空指针异常?

的活动

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

我可以看到我在做我看到同样的事情的XML从android sdk的tabs1 api示例。我试过“getLayoutInflator()”而不是“LayoutInflator.from(this)”,结果相同。

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

我已经检查,以确保R.layout.unit_view和tabHost不是null,当它运行LayoutInflater线,他们似乎很好。他们肯定不是空的。我也检查过,以确保LayoutInflater.from(this)返回一个有效的布局inflater对象,它确实如此。

指示错误的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,我的程序不会崩溃,但我也没有得到任何内容,只是选项卡。

回答

1

这确实对我的工作:

tabHost = getTabHost(); 

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

部分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 --> 
+0

这与重新添加“setContentView(R.layout.unit_view)” 感谢您的输入! – rushinge 2010-05-31 20:07:52

20

对于那些你谁正在使用TabHost不是TabActivity里面,那么一定要打电话TabHost.setup() 。以下是它的文档:http://developer.android.com/reference/android/widget/TabHost.html#setup() 当您尝试将选项卡添加到tabhost时,不这样做将导致NullPointerException。

本教程没有提及这一点,所以我想我会提醒您注意,为了节省您在我花费的时间寻找它。

+1

谢谢@Felix ..调用'TabHost.setup()'解决了我的问题。 – Zeba 2012-04-12 11:33:55

+0

谢谢@Felix也为我工作。 – 2012-04-23 08:52:27

0

只是想提供基于从这里陈述一些想法:

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

的TabWidget是导致飞机坠毁,如果你尝试加载数据之前绘制它。 为了防止出现这种情况,只需使用异步任务/处理程序不仅可以加载数据,还可以设置内容视图。 当然,您可以在数据加载过程中使用其他一些视图或进度对话框,只是不提供标签持有人。

0

年! 你需要调用:

TabHost.setup()

调用之前的方法。如果不是,则TabHost的TabWidget应该为空。 我犯了这么长时间的错误。

+0

它不起作用。 – 2015-10-06 18:05:30

相关问题