Android 2.1及更低版本的应用程序崩溃(NullPointerException)

时间:2012-04-26 06:53:34

标签: android nullpointerexception android-tabhost

我有一个FragmentActivity,带有以下onCreate方法,似乎在使用Android 2.1或更低版本的移动设备上崩溃。关于可能导致此错误的任何想法?

启动此活动时抛出的异常:

04-25 20:00:41.834: E/AndroidRuntime(381): Uncaught handler: thread main exiting due to  uncaught exception
04-25 20:00:41.877: E/AndroidRuntime(381): java.lang.NullPointerException 
04-25 20:00:41.877: E/AndroidRuntime(381):  at    android.widget.TabHost.dispatchWindowFocusChanged(TabHost.java:295)
04-25 20:00:41.877: E/AndroidRuntime(381):  at android.view.ViewGroup.dispatchWindowFocusChanged(ViewGroup.java:661)
04-25 20:00:41.877: E/AndroidRuntime(381):  at android.view.ViewGroup.dispatchWindowFocusChanged(ViewGroup.java:661)
04-25 20:00:41.877: E/AndroidRuntime(381):  at android.view.ViewGroup.dispatchWindowFocusChanged(ViewGroup.java:661)
04-25 20:00:41.877: E/AndroidRuntime(381):  at android.view.ViewGroup.dispatchWindowFocusChanged(ViewGroup.java:661)
04-25 20:00:41.877: E/AndroidRuntime(381):  at android.view.ViewRoot.handleMessage(ViewRoot.java:1819)
04-25 20:00:41.877: E/AndroidRuntime(381):  at android.os.Handler.dispatchMessage(Handler.java:99)
04-25 20:00:41.877: E/AndroidRuntime(381):  at android.os.Looper.loop(Looper.java:123)
04-25 20:00:41.877: E/AndroidRuntime(381):  at android.app.ActivityThread.main(ActivityThread.java:4363)
04-25 20:00:41.877: E/AndroidRuntime(381):  at java.lang.reflect.Method.invokeNative(Native Method)
04-25 20:00:41.877: E/AndroidRuntime(381):  at java.lang.reflect.Method.invoke(Method.java:521)
04-25 20:00:41.877: E/AndroidRuntime(381):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
04-25 20:00:41.877: E/AndroidRuntime(381):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
04-25 20:00:41.877: E/AndroidRuntime(381):  at dalvik.system.NativeStart.main(Native Method)

我的FragmentTabs类的一部分

public class FragmentTabs extends SherlockFragmentActivity
{
protected static final int RESULT_CLOSE_APPLICATION = 666;
protected static final int RESULT_RESET_USER = 667;
LoginController loginControl;
Context ctx;
Fragment lastFragment;
int initRun;
boolean isRoot;

/**
 * Decklare tabs and associate them with their respective initial fragments.
 */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.fragment_tabs);
    ActionBar bar = getSupportActionBar();
    bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
    bar.setDisplayShowHomeEnabled(false);
    bar.setDisplayShowTitleEnabled(false);
    ActionBar.Tab tabA = bar.newTab().setText("Forside");
    ActionBar.Tab tabB = bar.newTab().setText("Indstil");
    ActionBar.Tab tabC = bar.newTab().setText("Mere");

    tabA.setTabListener(new MyTabsListener(new FrontpageFragment()));
    tabB.setTabListener(new MyTabsListener(new SettingsFragment()));
    tabC.setTabListener(new MyTabsListener(new MoreFragment()));

    bar.addTab(tabA);
    bar.addTab(tabB);
    bar.addTab(tabC);

}

我尝试了here提供的答案,但我不认为该解决方案适用于我的应用。我认为它与“标签栏”有关。

2 个答案:

答案 0 :(得分:4)

解决方法可能是扩展Android提供的TabHost并检查mCurrentView是否为null,然后引用布局xml中的扩展类。

FixedTabHost.java:

package org.test.view;
public class FixedTabHost extends TabHost{

    public FixedTabHost(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public FixedTabHost(Context context) {
        super(context);
    }

    @Override
    public void dispatchWindowFocusChanged(boolean hasFocus) {
        // API LEVEL <7 occasionally throws a NPE here
        if(getCurrentView() != null){
            super.dispatchWindowFocusChanged(hasFocus);
        }

    }

}

layout.xml:

<org.test.view.FixedTabHost
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/tabhost"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

</org.test.view.FixedTabHost>

确保xml标记包含FixedTabHost.java的包!

答案 1 :(得分:0)

我遇到了同样的问题。虽然我无法最终解决这个问题,但我至少已经提出了一些可能有帮助的提示:

在Android 2.1中,方法TabHost.dispatchWindowFocusChanged尚未检查mCurrentView的空值:(来自Android源v2.1中的TabHost.java)

@Override
public void dispatchWindowFocusChanged(boolean hasFocus) {
    mCurrentView.dispatchWindowFocusChanged(hasFocus);
}

在Android 2.2及更高版本中,已插入此空值检查:(来自Android源v2.2中的TabHost.java)

@Override
public void dispatchWindowFocusChanged(boolean hasFocus) {
    if (mCurrentView != null){
        mCurrentView.dispatchWindowFocusChanged(hasFocus);
    }
}

为什么mCurrentView在这里是空的我不知道。但至少可以解释为什么它只发生在Android 2.1而不是2.2 +。