主页按钮后重新进入时应用程序崩溃。使用标签

时间:2012-11-13 17:54:45

标签: android android-tabhost

编辑:已解决。问题是tabHost.ClearAllTabs()。在调用此函数之前,我必须将currentActiveTab设置为0 - 0,如下所示:tabHost.setCurrentTab(0);

所以,对我来说解决方案是:

tabHost.setCurrentTab(0);
tabHost.clearAllTabs();

如果我每次调用onResume()时都没有调用clearAllTabs,那么除了那些已经完成创建大量标签的人之外,还创建了四个新标签...

编辑结束。

我的申请有问题。它是一个库项目并使用TabActivity,请参阅下面的代码。所有选项卡处理都在onResume()中,因为用户可以“暂停”(onStop())应用程序,连接到Internet并重新进入应用程序。

应用程序启动正常,如果使用后退按钮关闭,则重新启动。但是如果用户使用Home-button,则在ActivityOne的任何活动上“暂停”,重新进入时会出错。在第一个tabHost.addTab(spec)(在if语句之前)的代码中由NULLPOINTEREXCEPTION引起的错误是“无法恢复活动”。

Libraryproject = com.tabsactivity.library

此应用程序使用的所有布局和类都在库项目中。

ActivityZero在AndroidManifest.XML

中声明如下
<activity 
    android:name="com.tabsactivity.library.ActivityZero"
    android:windowSoftInputMode="stateHidden">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>
<activity
    android:name="com.tabsactivity.library.ActivityOne"
    android:windowSoftInputMode="stateHidden" >
</activity>
...
<activity
    android:name="com.tabsactivity.library.ActivityFour"
    android:windowSoftInputMode="stateHidden" >
</activity>

ActivityZero.class

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    res = getResources();
    appPref =  getSharedPreferences("Prefs", MODE_PRIVATE);
    prefEditor = appPref.edit(); 
    if (getApplicationContext().getPackageName().trim().equals("com.app")) prefEditor.putBoolean("demo", false);
        else prefEditor.putBoolean("demo", true).commit();      
    }
    @Override
    protected void onResume() {
        super.onResume();       
        ConnectivityManager conMgr = ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo ni = conMgr.getActiveNetworkInfo();
        if (ni == null) prefEditor.putBoolean("connected_net", false).commit();
            else prefEditor.putBoolean("connected_net", true).commit();
    this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);       

        tabHost = getTabHost();
        tabHost.clearAllTabs();

        intent = new Intent().setClass(this, com.tabsactivity.library.ActivityOne.class);               
        spec = tabHost.newTabSpec("One").setIndicator("One", res.getDrawable(R.drawable.ic_tab)).setContent(intent);
        tabHost.addTab(spec);

        if ( (appPref.getBoolean("connected_net", false) && appPref.getBoolean("demo", true)) || !appPref.getBoolean("demo", true)) {
            intent = new Intent().setClass(this, com.tabsactivity.library.ActivityTwo.class);
            spec = tabHost.newTabSpec("Two").setIndicator("Two", res.getDrawable(R.drawable.ic_tab)).setContent(intent);
            tabHost.addTab(spec);

            intent = new Intent().setClass(this, com.tabsactivity.library.ActivityThree.class);
            spec = tabHost.newTabSpec("Three").setIndicator("Three", res.getDrawable(R.drawable.ic_tab)).setContent(intent);
            tabHost.addTab(spec);

            intent = new Intent().setClass(this, com.tabsactivity.library.ActivityFour.class);
            spec = tabHost.newTabSpec("Four").setIndicator("Four", res.getDrawable(R.drawable.ic_tab)).setContent(intent);
            tabHost.addTab(spec);
        }   else {
                Toast.makeText(this, "Please make sure you have a working internetconnection!", Toast.LENGTH_LONG).show();
    }
    tabHost.setCurrentTab(0);
}

错误日志:

11-13 20:16:36.377: W/dalvikvm(9337): threadid=1: thread exiting with uncaught exception (group=0x400259f8)
11-13 20:16:36.387: E/AndroidRuntime(9337): FATAL EXCEPTION: main
11-13 20:16:36.387: E/AndroidRuntime(9337): java.lang.RuntimeException: Unable to resume activity {com.app/com.tabsactivity.library.ActivityZero}: java.lang.NullPointerException
11-13 20:16:36.387: E/AndroidRuntime(9337):     at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3347)
11-13 20:16:36.387: E/AndroidRuntime(9337):     at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3362)
11-13 20:16:36.387: E/AndroidRuntime(9337):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2162)
11-13 20:16:36.387: E/AndroidRuntime(9337):     at android.os.Handler.dispatchMessage(Handler.java:99)
11-13 20:16:36.387: E/AndroidRuntime(9337):     at android.os.Looper.loop(Looper.java:144)
11-13 20:16:36.387: E/AndroidRuntime(9337):     at android.app.ActivityThread.main(ActivityThread.java:4937)
11-13 20:16:36.387: E/AndroidRuntime(9337):     at java.lang.reflect.Method.invokeNative(Native Method)
11-13 20:16:36.387: E/AndroidRuntime(9337):     at java.lang.reflect.Method.invoke(Method.java:521)
11-13 20:16:36.387: E/AndroidRuntime(9337):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
11-13 20:16:36.387: E/AndroidRuntime(9337):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
11-13 20:16:36.387: E/AndroidRuntime(9337):     at dalvik.system.NativeStart.main(Native Method)
11-13 20:16:36.387: E/AndroidRuntime(9337): Caused by: java.lang.NullPointerException
11-13 20:16:36.387: E/AndroidRuntime(9337):     at android.widget.TabWidget.setCurrentTab(TabWidget.java:339)
11-13 20:16:36.387: E/AndroidRuntime(9337):     at android.widget.TabWidget.focusCurrentTab(TabWidget.java:363)
11-13 20:16:36.387: E/AndroidRuntime(9337):     at android.widget.TabHost.setCurrentTab(TabHost.java:320)
11-13 20:16:36.387: E/AndroidRuntime(9337):     at android.widget.TabHost.addTab(TabHost.java:213)
11-13 20:16:36.387: E/AndroidRuntime(9337):     at com.tabsactivity.library.ActivityZero.onResume(ActivityZero.java:209)
11-13 20:16:36.387: E/AndroidRuntime(9337):     at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1255)
11-13 20:16:36.387: E/AndroidRuntime(9337):     at android.app.Activity.performResume(Activity.java:3864)
11-13 20:16:36.387: E/AndroidRuntime(9337):     at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3337)
11-13 20:16:36.387: E/AndroidRuntime(9337):     ... 10 more
11-13 20:16:36.437: D/dalvikvm(9337): GC_FOR_MALLOC freed 5320 objects / 572584 bytes in 48ms

0 个答案:

没有答案