我有一个带有以下onCreate方法的FragmentActivity,该方法似乎在使用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);
}
我尝试了此处提供的答案,但我认为该解决方案不适用于我的应用程序。我确实认为它与"标签栏"有关。
解决方法可能是扩展 Android 提供的 TabHost 并检查 mCurrentView 是否为空,然后在布局 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);
}
}
}
布局.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!
我遇到了同样的问题。虽然我最终无法解决它,但我至少已经有一些提示可能会有所帮助:
在 Android 2.1 中,方法 TabHost.dispatchWindowFocusChanged 尚未检查 mCurrentView 的空值:(来自 TabHost.java在 Android 源代码 v2.1 中)
@Override
public void dispatchWindowFocusChanged(boolean hasFocus) {
mCurrentView.dispatchWindowFocusChanged(hasFocus);
}
在 Android 2.2 及更高版本中,已插入此空值检查:(来自 TabHost.java在 Android 源代码 v2.2 中)
@Override
public void dispatchWindowFocusChanged(boolean hasFocus) {
if (mCurrentView != null){
mCurrentView.dispatchWindowFocusChanged(hasFocus);
}
}
我不知道为什么mCurrentView在这里为空。但这至少解释了为什么它只发生在 Android 2.1 中而不是在 2.2+ 中。