Android TabActivity - 无法恢复活动; onResume()方法&空指针异常

时间:2014-08-21 08:05:49

标签: android samsung-mobile tabactivity

我有一个有趣的Android开发问题。 我在我的应用程序中使用TabActivity(我知道它已被弃用),这使得可以在3个选项卡之间进行选择和导航(手动和自动处理以及配置选项卡;应用程序的主要目标现在不相关)。 / p>

前几天我在其他类型的设备上测试了该应用程序:在阿尔卡特手机上,不记得确切的类型;之前在三星Galaxy S4和其他三星旧设备上;不过,还有一款无名的中文Android平板电脑。 这些设备都没有TabActivity的问题。

但是现在,今天我尝试在三星Galaxy S2和HTC Desire S上使用该应用程序,并且两台设备都崩溃了,因为使用TabActivity

为了更容易想象GUI:三个选项卡,第三个是带有一些子菜单的菜单。在三个基本选项卡之间导航不会产生任何问题。但是:如果我将第三个标签放到其中一个子菜单中,然后尝试返回菜单(已实施,或实体'返回'按钮,并不重要),应用程序崩溃

错误消息如下:

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

一些可能很重要的Java代码:

@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);
    }

试图找到类似的问题,但我没有成功。

任何想法?
提前致谢。 :)

最好的问候,
ZS

调试窗口

<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)

2 个答案:

答案 0 :(得分:0)

将用于初始化制表符意图的代码更改为

    tabspecAutoTab = mTabHost.newTabSpec("auto")
            .setIndicator(res.getString(R.string.title_pagecontname))
            .setContent(new Intent(getApplicationContext(), AutoTab.class));
    tabspecManualTab = mTabHost.newTabSpec("manual")
            .setIndicator(res.getString(R.string.title_pagemanname))
            .setContent(new Intent(getApplicationContext(), ManualTab.class));
    tabspecConfigTab = mTabHost.newTabSpec("config")
            .setIndicator(res.getString(R.string.title_pageconfname))
            .setContent(new Intent(getApplicationContext(), ConfigTab.class));

答案 1 :(得分:0)

经过11个小时的研究,尝试,工作和痛苦的答案在这里找到:
Removing a tab and the activity (intent) inside of it from a TabHost
(非常感谢 wired00

onResume()方法需要以下行:

mTabHost.setCurrentTab(0);

之前:

mTabHost.clearAllTabs();