I have a FragmentActivity with the following onCreate method that seems to crash on mobile devices using Android 2.1 or below. Any ideas on what could be causing this error ?
The exception being thrown when this activity is started :
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)
And part of my FragmentTabs class
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);
}
I tried the answer provided here but I don’t think the solution applies to my app. I do think it is related to the “Tab bar”.
I’m having the same issue. While I could not finally solve it, I at least have some hint already that might help:
In Android 2.1, the method TabHost.dispatchWindowFocusChanged did not yet check for null values of mCurrentView: (from TabHost.java in the Android source v2.1)
In Android 2.2 and later, this check for a null value has been inserted: (from TabHost.java in the Android source v2.2)
Why exactly mCurrentView is null here I don’t know. But this at least explains why it only happens in Android 2.1 and not in 2.2+.