1

我想用4個小工具使用tabhost,我已經測試了這個在android 4.2上,它的工作原理就像是一個魅力,然而在android 2.2上它給了我一個nullpointerexception當我改變標籤TabHost在Android 2.2中的空指針異常

我知道有一個tabhosts和android 2.1/2.2的已知問題,但我似乎無法讓它與人們在其他線程上建議的其他修復程序一起工作。

注:我使用Android Annotations

這裏的logcat的:

03-23 10:29:08.869: E/AndroidRuntime(423): FATAL EXCEPTION: main 
03-23 10:29:08.869: E/AndroidRuntime(423): java.lang.NullPointerException 
03-23 10:29:08.869: E/AndroidRuntime(423): at android.widget.TabWidget.focusCurrentTab(TabWidget.java:367) 
03-23 10:29:08.869: E/AndroidRuntime(423): at android.widget.TabHost.setCurrentTab(TabHost.java:320) 
03-23 10:29:08.869: E/AndroidRuntime(423): at android.widget.TabHost$2.onTabSelectionChanged(TabHost.java:129) 
03-23 10:29:08.869: E/AndroidRuntime(423): at android.widget.TabWidget$TabClickListener.onClick(TabWidget.java:453) 
03-23 10:29:08.869: E/AndroidRuntime(423): at android.view.View.performClick(View.java:2408) 
03-23 10:29:08.869: E/AndroidRuntime(423): at android.view.View$PerformClick.run(View.java:8816) 
03-23 10:29:08.869: E/AndroidRuntime(423): at android.os.Handler.handleCallback(Handler.java:587) 
03-23 10:29:08.869: E/AndroidRuntime(423): at android.os.Handler.dispatchMessage(Handler.java:92) 
03-23 10:29:08.869: E/AndroidRuntime(423): at android.os.Looper.loop(Looper.java:123) 
03-23 10:29:08.869: E/AndroidRuntime(423): at android.app.ActivityThread.main(ActivityThread.java:4627) 
03-23 10:29:08.869: E/AndroidRuntime(423): at java.lang.reflect.Method.invokeNative(Native Method) 
03-23 10:29:08.869: E/AndroidRuntime(423): at java.lang.reflect.Method.invoke(Method.java:521) 
03-23 10:29:08.869: E/AndroidRuntime(423): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 
03-23 10:29:08.869: E/AndroidRuntime(423): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 
03-23 10:29:08.869: E/AndroidRuntime(423): at dalvik.system.NativeStart.main(Native Method) 

這裏是我的代碼:

@NoTitle 
@EActivity(R.layout.activity_base) 
public class BaseActivity extends TabActivity { 

    Context mContext; 

    @ViewById 
    Button btnBaseDeconnecter; 

    @ViewById 
    TextView txtBaseInfos; 

    @AfterViews 
    void afterViews() { 
     mContext = this; 
     TabHost tabHost = getTabHost(); 

     /* tid1 is firstTabSpec Id. Its used to access outside. */ 
     TabSpec firstTabSpec = tabHost.newTabSpec("0"); 
     TabSpec secondTabSpec = tabHost.newTabSpec("1"); 
     TabSpec thirdTabSpec = tabHost.newTabSpec("2"); 
     TabSpec fourthTabSpec = tabHost.newTabSpec("3"); 


     /* TabSpec setIndicator() is used to set name for the tab. */ 
     /* TabSpec setContent() is used to set content for a particular tab. */ 
     firstTabSpec.setIndicator(
       prepareTabView(mContext, getResources().getDrawable(R.drawable.enlevement))).setContent(
       new Intent(this, EnlevementActivity_.class)); 
     secondTabSpec.setIndicator(
       prepareTabView(mContext, getResources().getDrawable(R.drawable.travaux))).setContent(
       new Intent(this, TravauxListActivity_.class)); 
     thirdTabSpec.setIndicator(
       prepareTabView(mContext, getResources().getDrawable(R.drawable.compte))).setContent(
       new Intent(this, CompteActivity_.class)); 
     fourthTabSpec.setIndicator(
       prepareTabView(mContext, getResources().getDrawable(R.drawable.contact))).setContent(
       new Intent(this, ContactActivity_.class)); 
     tabHost.setup(); 
     /* Add tabSpec to the TabHost to display. */ 
     tabHost.addTab(firstTabSpec); 
     tabHost.addTab(secondTabSpec); 
     tabHost.addTab(thirdTabSpec); 
     tabHost.addTab(fourthTabSpec); 
     tabHost.getTabWidget().setStripEnabled(false); 
     tabHost.getTabWidget().setDividerDrawable(R.drawable.empty_divider); 


    } 
    public static View prepareTabView(Context context, Drawable background) { 
     View view = LayoutInflater.from(context).inflate(R.layout.fake_native_tab, null); 
     ImageView img = (ImageView)view.findViewById(R.id.fakeNativeTabImageView); 
     img.setImageDrawable(background); 
     return view; 
    } 




} 
+0

是否發生這種異常?是否嘗試打開特定選項卡或嘗試打開任何選項卡或四個選項卡? – 2013-03-23 09:47:21

+0

當應用程序按照您的預期啓動時,會在第一個選項卡上加載,但如果我點擊第二個選項卡,則會將其發送給第三個選項卡,並且當我單擊第三個或第四個選項卡時,它會崩潰 – 2013-03-23 09:51:31

+0

Precsions:它發送我到第三個小部件,但到第二個類 – 2013-03-23 10:17:52

回答

3

使用tabHost.getTabWidget().setDividerDrawable(null);而不是R.drawable.empty_divider

推理:

望着堆棧跟蹤和Android 2.2 source for TabWidget.java,似乎在那裏,如果dividerDrawable不爲空,那麼它跳過標籤欄,以獲得實際的分頻器的看法是在getChildTabViewAt(int index)方法的問題TabView的。

public View getChildTabViewAt(int index) { 
    // If we are using dividers, then instead of tab views at 0, 1, 2, ... 
    // we have tab views at 0, 2, 4, ... 
    if (mDividerDrawable != null) { 
     index *= 2; 
    } 
    return getChildAt(index); 
} 

我認爲當你使用R.drawable.empty_divider的dividerDrawable分頻器意見不要糾纏會發生什麼,所以,如果他們null他們應及時治療。正如你所看到的那樣,這不會發生,所以這將解釋NullPointerException以及單擊一個選項卡並選擇另一個選項卡的奇怪行爲。

0

使用backword兼容庫手段添加android.support.v4.jar在libs文件夾中,並嘗試使用該庫中的同一類,它將兼容較低版本和較高版本

+0

android.support.v4.jar庫不包含一個類TabHost或TabHostActivity – 2013-03-23 10:14:00