java.lang.IllegalStateException:已经调用了commit

时间:2015-06-26 13:03:31

标签: java android

我尝试过以下代码,

try {
    final Activity activity = ctx;
    FragmentTransaction ft = activity.getFragmentManager().beginTransaction();
    android.app.Fragment prev = activity.getFragmentManager().findFragmentByTag("dialog");
    if (prev != null) {
        ft.remove(prev);
    }
    DialogServiceFailed newFragment = DialogServiceFailed.newInstance(pageName, onServiceFailed);
    newFragment.show(ft, "dialog");
    ft.addToBackStack(null);
    ft.commitAllowingStateLoss();
} catch (ClassCastException e) {
    Log.d("Log", "Can't get the fragment manager with this");
}

但是我收到以下异常,我的应用程序崩溃了。

java.lang.IllegalStateException: commit already called
        at android.app.BackStackRecord.commitInternal(BackStackRecord.java:582)
        at android.app.BackStackRecord.commitAllowingStateLoss(BackStackRecord.java:578)
        at com.brightspot.extrain5psim.model.APIRequestHandler.loadServiceFailedDialog(APIRequestHandler.java:99)
        at com.brightspot.extrain5psim.view.fragments.LoginFragment.setOnAsyncTaskCompleted(LoginFragment.java:201)
        at com.brightspot.extrain5psim.model.APIRequestHandler.onPostExecute(APIRequestHandler.java:80)
        at com.brightspot.extrain5psim.model.APIRequestHandler.onPostExecute(APIRequestHandler.java:17)
        at android.os.AsyncTask.finish(AsyncTask.java:631)
        at android.os.AsyncTask.access$600(AsyncTask.java:177)
        at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:176)
        at android.app.ActivityThread.main(ActivityThread.java:5317)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
        at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:7)

newFragment.show(ft, "dialog")

显示内部提交的调用。所以你可能想要摆脱

ft.commitAllowingStateLoss();

或者你可以摆脱

newFragment.show(ft, "dialog");

添加电话

ft.add(newFragment, "dialog");
ft.commitAllowingStateLoss();

修改

这是DialogFragment的{​​{1}}看起来像

show()