2010-05-31 94 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

相關問題