I've got an interesting Android developing problem. I use TabActivity (I know it's deprecated) in my app which makes it possible to choose and navigate between 3 tabs (manual and automatic processing, and configuration tab; the main goal of the application is not relevant now).
Few days ago I tested the application on other kind of devices: on an Alcatel phone, do not remember the exact type; before that on a Samsung Galaxy S4, and other, older Samsung devices; still, there was a no-name chinese Android tablet as well. None of these devices had a problem with TabActivity.
But now, today I've tried to use the application on a Samsung Galaxy S2 and on an HTC Desire S, and both devices have crashed 'cause of using TabActivity.
To make it easier to imagine the GUI: three tabs, the third one is a menu with some submenus. Navigating between the three basic tabs doesn't make any problem. BUT: if I go on the third tab into one of the submenus, and then try coming back to the menu (implemented, or physical 'Back' button, doesn't matter), the application crashes.
The error messages are the following:
08-21 09:46:18.170: E/AndroidRuntime(7781): FATAL EXCEPTION: main
08-21 09:46:18.170: E/AndroidRuntime(7781): java.lang.RuntimeException: Unable to resume activity {com.example.MY_PROJECT/com.example.MY_PROJECT.ControlActivity}: java.lang.NullPointerException
08-21 09:46:18.170: E/AndroidRuntime(7781): at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2571)
08-21 09:46:18.170: E/AndroidRuntime(7781): at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2592)
08-21 09:46:18.170: E/AndroidRuntime(7781): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1080)
08-21 09:46:18.170: E/AndroidRuntime(7781): at android.os.Handler.dispatchMessage(Handler.java:99)
08-21 09:46:18.170: E/AndroidRuntime(7781): at android.os.Looper.loop(Looper.java:150)
08-21 09:46:18.170: E/AndroidRuntime(7781): at android.app.ActivityThread.main(ActivityThread.java:4385)
08-21 09:46:18.170: E/AndroidRuntime(7781): at java.lang.reflect.Method.invokeNative(Native Method)
08-21 09:46:18.170: E/AndroidRuntime(7781): at java.lang.reflect.Method.invoke(Method.java:507)
08-21 09:46:18.170: E/AndroidRuntime(7781): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:849)
08-21 09:46:18.170: E/AndroidRuntime(7781): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:607)
08-21 09:46:18.170: E/AndroidRuntime(7781): at dalvik.system.NativeStart.main(Native Method)
08-21 09:46:18.170: E/AndroidRuntime(7781): Caused by: java.lang.NullPointerException
08-21 09:46:18.170: E/AndroidRuntime(7781): at android.widget.TabWidget.setCurrentTab(TabWidget.java:344)
08-21 09:46:18.170: E/AndroidRuntime(7781): at android.widget.TabWidget.focusCurrentTab(TabWidget.java:368)
08-21 09:46:18.170: E/AndroidRuntime(7781): at android.widget.TabHost.setCurrentTab(TabHost.java:323)
08-21 09:46:18.170: E/AndroidRuntime(7781): at android.widget.TabHost.addTab(TabHost.java:216)
08-21 09:46:18.170: E/AndroidRuntime(7781): at com.example.MY_PROJECT.ControlActivity.setTabs(ControlActivity.java:186)
08-21 09:46:18.170: E/AndroidRuntime(7781): at com.example.MY_PROJECT.ControlActivity.onResume(ControlActivity.java:160)
08-21 09:46:18.170: E/AndroidRuntime(7781): at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1242)
08-21 09:46:18.170: E/AndroidRuntime(7781): at android.app.Activity.performResume(Activity.java:4004)
08-21 09:46:18.170: E/AndroidRuntime(7781): at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2561)
08-21 09:46:18.170: E/AndroidRuntime(7781): ... 10 more
Some Java code that could be important:
@Override
protected void onResume() {
super.onResume();
Resources res = getResources();
this.setTitle(res.getString(R.string.title_activity_main_menu));
mTabHost.clearAllTabs();
setTabs();
MainActivity.isAnotherDevSel = false;
}
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
initial(); //Initializing variables
mTabHost = getTabHost();
setTabs();
}
public void setTabs() {
tempPos = position;
Resources res = getResources();
tabspecAutoTab = mTabHost.newTabSpec("auto")
.setIndicator(res.getString(R.string.title_pagecontname))
.setContent(new Intent(ControlActivity.this, AutoTab.class));
tabspecManualTab = mTabHost.newTabSpec("manual")
.setIndicator(res.getString(R.string.title_pagemanname))
.setContent(new Intent(ControlActivity.this, ManualTab.class));
tabspecConfigTab = mTabHost.newTabSpec("config")
.setIndicator(res.getString(R.string.title_pageconfname))
.setContent(new Intent(ControlActivity.this, ConfigTab.class));
mTabHost.addTab(tabspecAutoTab);
mTabHost.addTab(tabspecManualTab);
mTabHost.addTab(tabspecConfigTab);
mTabHost.setOnTabChangedListener(new OnTabChangeListener() {
@Override
public void onTabChanged(String tabId) {
if ("auto".equals(tabId)) {
ManualTab.isManualInfo = false;
position = 0;
}
else if ("manual".equals(tabId)) {
AutoTab.isAutoInfo = false;
position = 1;
}
if ("config".equals(tabId)
&& (AutoTab.isAutoRunning || ManualTab.isManualRunning)) {
AlertDialog.Builder alertDialog = new AlertDialog.Builder(
ControlActivity.this);
alertDialog.setTitle(R.string.running_program);
alertDialog.setMessage(R.string.not_use_config);
alertDialog.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int which) {
if (AutoTab.isAutoRunning) {
mTabHost.setCurrentTab(0);
} else if (ManualTab.isManualRunning) {
mTabHost.setCurrentTab(1);
}
}
});
alertDialog.setCancelable(false);
AlertDialog alertDialogBuilder = alertDialog.create();
alertDialogBuilder.show();
}
}
});
mTabHost.setCurrentTab(tempPos);
}
Tried to find similar questions but I wasn't successful.
Any idea?
Thanks in advance. :)
Best regards,
Zs
Debug window:
<terminated>MY_PROJECT [Android Application]
<disconnected>DalvikVM[localhost:8600]
MY_PROJECT [Android Application]
DalvikVM[localhost:8600]
Thread [<1> main] (Suspended (exception NullPointerException))
<VM does not provide monitor information>
TabWidget.setCurrentTab(int) line: 344
TabWidget.focusCurrentTab(int) line: 368
TabHost.setCurrentTab(int) line: 323
TabHost.addTab(TabHost$TabSpec) line: 216
ControlActivity.setTabs() line: 211
ControlActivity.onResume() line: 160
Instrumentation.callActivityOnResume(Activity) line: 1242
ControlActivity(Activity).performResume() line: 4004
ActivityThread.performResumeActivity(IBinder, boolean) line: 2561
ActivityThread.handleResumeActivity(IBinder, boolean, boolean) line: 2592
BinderProxy(ActivityThread$H).handleMessage(Message) line: 1080
ActivityThread$H(Handler).dispatchMessage(Message) line: 99
Looper.loop() line: 150
ActivityThread.main(String[]) line: 4385
Method.invokeNative(Object, Object[], Class, Class[], Class, int, boolean) line: not available [native method]
Method.invoke(Object, Object...) line: 507
ZygoteInit$MethodAndArgsCaller.run() line: 849
ZygoteInit.main(String[]) line: 607
NativeStart.main(String[]) line: not available [native method]
Thread [<8> Binder Thread #2] (Running)
Thread [<7> Binder Thread #1] (Running)
Thread [<10> AsyncTask #1] (Running)
Thread [<11> Binder Thread #3] (Running)
Thread [<12> Thread-12] (Running)
setTabs():
this ControlActivity (id=830007806776)
address "00:14:03:18:68:92" (id=830007826024)
bluetoothAdapter BluetoothAdapter (id=830008015408)
btDevice BluetoothDevice (id=830008071648)
btMessage "0/ 3.52/ 0.00\r" (id=830007771352)
bundle Bundle (id=830008071800)
dsm DefaultSettingsManager (id=830008395608)
handler ControlActivity$1 (id=830007939176)
isNotInFront true
mActivity_dispatchPenEvent null
mActivity_onPenEvent null
mActivityInfo ActivityInfo (id=830008207600)
mApplication Application (id=830007937232)
mBase ContextImpl (id=830008089024)
mBase ContextImpl (id=830008089024)
mCalled true
mComponent ComponentName (id=830008207352)
mConfigChangeFlags 0
mCurrentConfig Configuration (id=830008198384)
mDecor PhoneWindow$DecorView (id=830008078448)
mDefaultKeyMode 0
mDefaultKeySsb null
mDefaultTab null
mDefaultTabIndex -1
mEmbeddedID null
mExecuteFastLaunch false
mFinished false
mHandler Handler (id=830008065464)
mIdent 1082549496
mInflater PhoneLayoutInflater (id=830008198232)
mInstrumentation Instrumentation (id=830007792592)
mIntent Intent (id=830008207200)
mLastNonConfigurationChildInstances null
mLastNonConfigurationInstance null
mLocalActivityManager LocalActivityManager (id=830007825000)
mMainThread ActivityThread (id=830007781896)
mManagedCursors ArrayList (id=830008207176)
mManagedDialogs null
mMenuSize 0
mParent null
mResultCode 0
mResultData null
mResumed true
mSearchManager null
mStartedActivity false
mStopped false
mTabHost TabHost (id=830008193424)
mTheme Resources$Theme (id=830008073688)
mThemeResource 2131427447
mTitle " (id=830007786896)" (id=830007786896)
mTitleColor 0
mTitleReady true
mToken BinderProxy (id=830008207544)
mUiThread Thread (id=830002591192)
mVisibleFromClient true
mVisibleFromServer false
mWindow PhoneWindow (id=830008199768)
mWindowAdded true
mWindowManager Window$LocalWindowManager (id=830008089384)
MY_UUID UUID (id=830008067000)
progressDialog ProgressDialog (id=830007765240)
releaseMemoryRunnable Activity$1 (id=830007961136)
res Resources (id=830007936080)
tabspecAutoTab TabHost$TabSpec (id=830008573904)
tabspecConfigTab TabHost$TabSpec (id=830008574224)
tabspecManualTab TabHost$TabSpec (id=830008574064)
tempPos 2
res Resources (id=830007936080)
onResume():
this ControlActivity (id=830007806776)
address "00:14:03:18:68:92" (id=830007826024)
bluetoothAdapter BluetoothAdapter (id=830008015408)
btDevice BluetoothDevice (id=830008071648)
btMessage "0/ 3.52/ 0.00\r" (id=830007771352)
bundle Bundle (id=830008071800)
dsm DefaultSettingsManager (id=830008395608)
handler ControlActivity$1 (id=830007939176)
isNotInFront true
mActivity_dispatchPenEvent null
mActivity_onPenEvent null
mActivityInfo ActivityInfo (id=830008207600)
mApplication Application (id=830007937232)
mBase ContextImpl (id=830008089024)
mBase ContextImpl (id=830008089024)
mCalled true
mComponent ComponentName (id=830008207352)
mConfigChangeFlags 0
mCurrentConfig Configuration (id=830008198384)
mDecor PhoneWindow$DecorView (id=830008078448)
mDefaultKeyMode 0
mDefaultKeySsb null
mDefaultTab null
mDefaultTabIndex -1
mEmbeddedID null
mExecuteFastLaunch false
mFinished false
mHandler Handler (id=830008065464)
mIdent 1082549496
mInflater PhoneLayoutInflater (id=830008198232)
mInstrumentation Instrumentation (id=830007792592)
mIntent Intent (id=830008207200)
mLastNonConfigurationChildInstances null
mLastNonConfigurationInstance null
mLocalActivityManager LocalActivityManager (id=830007825000)
mMainThread ActivityThread (id=830007781896)
mManagedCursors ArrayList (id=830008207176)
mManagedDialogs null
mMenuSize 0
mParent null
mResultCode 0
mResultData null
mResumed true
mSearchManager null
mStartedActivity false
mStopped false
mTabHost TabHost (id=830008193424)
mTheme Resources$Theme (id=830008073688)
mThemeResource 2131427447
mTitle " (id=830007786896)" (id=830007786896)
mTitleColor 0
mTitleReady true
mToken BinderProxy (id=830008207544)
mUiThread Thread (id=830002591192)
mVisibleFromClient true
mVisibleFromServer false
mWindow PhoneWindow (id=830008199768)
mWindowAdded true
mWindowManager Window$LocalWindowManager (id=830008089384)
MY_UUID UUID (id=830008067000)
progressDialog ProgressDialog (id=830007765240)
releaseMemoryRunnable Activity$1 (id=830007961136)
res Resources (id=830007936080)
tabspecAutoTab TabHost$TabSpec (id=830008573904)
tabspecConfigTab TabHost$TabSpec (id=830008574224)
tabspecManualTab TabHost$TabSpec (id=830008574064)
tempPos 2
res Resources (id=830007936080)
After 11 hours research, trying, work and suffering the answer found here:
Removing a tab and the activity (intent) inside of it from a TabHost
(A lot of thanks to wired00)
The following line was needed in the onResume() method:
mTabHost.setCurrentTab(0);
right before:
mTabHost.clearAllTabs();