0

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

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

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

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

编辑结束。

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

如果应用程序使用后退按钮关闭,应用程序可以正常启动并重新启动。但是,如果用户使用主页按钮,在任何活动上“暂停”放置 ActivityOne,我在重新输入时会收到错误消息。错误是由我的代码中的第一个 tabHost.addTab(spec) 中的 NULLPOINTEREXCEPTION 引起的“无法恢复活动”(在 if 语句之前)。

图书馆项目 = com.tabsactivity.library

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

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
4

0 回答 0