android 2.2中的TabHost nullpointer异常

时间:2013-03-23 09:39:09

标签: android nullpointerexception android-tabhost android-2.2-froyo android-annotations

我正在尝试使用带有4个小部件的tabhost,我已经在android 4.2上测试了它,它就像一个魅力,但是在Android 2.2上,当我更改标签时它给了我一个nullpointerexception

我知道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;
    }




}

2 个答案:

答案 0 :(得分:3)

使用tabHost.getTabWidget().setDividerDrawable(null);代替R.drawable.empty_divider

推理:

查看堆栈跟踪和Android 2.2 source for TabWidget.javagetChildTabViewAt(int index)方法中似乎存在一个问题,如果dividerDrawable不为null,它会跳过选项卡栏中的分隔符视图以获取实际的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以及单击一个选项卡并选择另一个选项卡的奇怪行为。

答案 1 :(得分:0)

使用backword兼容库意味着在libs文件夹中添加android.support.v4.jar并尝试使用此库中的相同类,它将兼容较低版本和较高版本