java. Lang. IllegalStateException: commit has been called
•
Java
I tried the following code,
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");
}
But I received the following exception and my application crashed
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)
Solution
newFragment.show(ft,"dialog")
newFragment.show(ft,"dialog")
Show calls internal submission So you might want to get rid of it
ft.commitAllowingStateLoss();
Or you can get rid of it
newFragment.show(ft,"dialog");
Add phone
ft.add(newFragment,"dialog"); ft.commitAllowingStateLoss();
edit
This is what dialogfragment's show () looks like
public int show(FragmentTransaction transaction,String tag) {
mDismissed = false;
mShownByMe = true;
transaction.add(this,tag);
mViewDestroyed = false;
mBackStackId = transaction.commit();
return mBackStackId;
}
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码
