获取'java.lang.IllegalStateException:活动已被销毁',同时在旋转后隐藏DialogFragment

时间:2014-09-03 17:56:26

标签: android android-fragments android-actionbar android-actionbaractivity

我有一个ActionBarActivity,有时会显示DialogFragment。当我在旋转后尝试关闭对话框时,它崩溃并出现IllegalStateException:Activity已被销毁。我无法将Activity设置为

            android:configChanges="orientation|screenSize|keyboardHidden"

因为我需要在旋转时重新绘制布局。

/**
 * Shows Loading progress dialog
 */
private void showProgress() {
    FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
    Fragment prev = getSupportFragmentManager().findFragmentByTag(LoadingDialogFragment.FRAGMENTMANAGERTAG);
    if (prev != null) {
        ft.remove(prev);
    }
    // Create and show the dialog.
    mProgressDialog = LoadingDialogFragment.newInstance("", getString(R.string.string_loading));
    mProgressDialog.show(ft, LoadingDialogFragment.FRAGMENTMANAGERTAG);
}

private void hideProgress() {
    if (mProgressDialog != null) {
        Log.d(getClass().getSimpleName(), "hideDialogNoConnection quita dialog");
        FragmentManager manager = getSupportFragmentManager();
        manager.beginTransaction().remove(mProgressDialog).addToBackStack(null).commitAllowingStateLoss();
        Log.d(getClass().getSimpleName(), "hideDialogNoConnection quita dialog con popBackStack");

        manager.popBackStack();
    }
    FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
    Fragment prev1 = getSupportFragmentManager().findFragmentByTag(LoadingDialogFragment.FRAGMENTMANAGERTAG);
    if (prev1 != null) {
        ft.remove(prev1);
    }
}

我该怎么办?

- Edit-- 这是logcat:

09-03 20:00:39.102: E/AndroidRuntime(10917): FATAL EXCEPTION: main
09-03 20:00:39.102: E/AndroidRuntime(10917): Process: com.myapp.launcher, PID: 10917
09-03 20:00:39.102: E/AndroidRuntime(10917): java.lang.IllegalStateException: Activity has been destroyed
09-03 20:00:39.102: E/AndroidRuntime(10917):    at android.support.v4.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1376)
09-03 20:00:39.102: E/AndroidRuntime(10917):    at android.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java:595)
09-03 20:00:39.102: E/AndroidRuntime(10917):    at android.support.v4.app.BackStackRecord.commitAllowingStateLoss(BackStackRecord.java:578)
09-03 20:00:39.102: E/AndroidRuntime(10917):    at com.myapp.launcher.activities.HomeActivity.hideProgress(HomeActivity.java:348)
09-03 20:00:39.102: E/AndroidRuntime(10917):    at com.myapp.launcher.activities.HomeActivity.access$0(HomeActivity.java:344)
09-03 20:00:39.102: E/AndroidRuntime(10917):    at com.myapp.launcher.activities.HomeActivity$2.handleMessage(HomeActivity.java:157)
09-03 20:00:39.102: E/AndroidRuntime(10917):    at android.os.Handler.dispatchMessage(Handler.java:102)
09-03 20:00:39.102: E/AndroidRuntime(10917):    at android.os.Looper.loop(Looper.java:136)
09-03 20:00:39.102: E/AndroidRuntime(10917):    at android.app.ActivityThread.main(ActivityThread.java:5479)
09-03 20:00:39.102: E/AndroidRuntime(10917):    at java.lang.reflect.Method.invokeNative(Native Method)
09-03 20:00:39.102: E/AndroidRuntime(10917):    at java.lang.reflect.Method.invoke(Method.java:515)
09-03 20:00:39.102: E/AndroidRuntime(10917):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
09-03 20:00:39.102: E/AndroidRuntime(10917):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)

0 个答案:

没有答案