Android 致命错误 - 无法在 onSaveInstanceState 之后执行此操作
Android Fatal Error - Can not perform this action after onSaveInstanceState
我在 Android 中请求权限后尝试执行片段事务 - 但是应用程序不断崩溃。为了避免这种情况,如何更改此代码?
如有任何帮助,我们将不胜感激。
发送电子邮件前检查权限:
private void emailShare() {
if (mayRequestContacts()) {
emailShareImpl();
}
}
检查权限:
private boolean mayRequestContacts() {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
return true;
}
requestPermissions(new String[]{READ_CONTACTS}, REQUEST_READ_CONTACTS);
return false;
}
权限检查结果:
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
if (requestCode == REQUEST_READ_CONTACTS) {
if (grantResults.length == 1 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
emailShareImpl();
}
}
}
如果获得许可则启动片段交易:
private void emailShareImpl() {
FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
EmailReferDialog mEmailReferDialog = new EmailReferDialog();
mEmailReferDialog.setCancelable(true);
mEmailReferDialog.show(fragmentManager, "EMAIL_REFER_DIALOG");
}
LOGCAT:
05-11 09:04:58.859 16113-16113/com.example E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example, PID: 16113
java.lang.RuntimeException: Failure delivering result ResultInfo{who=@android:requestPermissions:, request=65536, result=-1, data=Intent { act=android.content.pm.action.REQUEST_PERMISSIONS (has extras) }} to activity {com.example/com.example.DashboardActivity}: java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
at android.app.ActivityThread.deliverResults(ActivityThread.java:3743)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:3786)
at android.app.ActivityThread.-wrap16(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1412)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5466)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
at android.support.v4.app.FragmentManagerImpl.checkStateLoss(FragmentManager.java:1493)
at android.support.v4.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1511)
at android.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java:638)
at android.support.v4.app.BackStackRecord.commit(BackStackRecord.java:617)
at android.support.v4.app.DialogFragment.show(DialogFragment.java:139)
at com.example.ReferFragment.emailShareImpl(ReferFragment.java:222)
at com.example.ReferFragment.onRequestPermissionsResult(ReferFragment.java:140)
at android.support.v4.app.FragmentActivity.onRequestPermissionsResult(FragmentActivity.java:894)
at android.app.Activity.dispatchRequestPermissionsResult(Activity.java:6582)
at android.app.Activity.dispatchActivityResult(Activity.java:6460)
at android.app.ActivityThread.deliverResults(ActivityThread.java:3739)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:3786)
at android.app.ActivityThread.-wrap16(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1412)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5466)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
发生这种情况是因为 onRequestPermissionresult
在 activity 的 onResume
之前被调用。您可以通过以下方式简单地解决此问题:
private boolean mShareEmail = false;
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
if (requestCode == REQUEST_READ_CONTACTS) {
if (grantResults.length == 1 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
mShareEmail = true;
}
}
}
@Override
public void onResume() {
super.onResume();
if (mShareEmail) {
emailShareImpl();
mShareEmail = false;
}
}
也许下面的提示有点晚了,但不是解决方法,如果你不使用 activity 的状态(例如,权限模型应该是无状态的),你应该使用commitAllowingStateLoss 在片段转换期间。
我通过添加处理程序解决了这个问题:
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
new FolderChooserDialog.Builder(ReportActivity.this)
.tag(REQUEST_CODE_Excel+"")
.show();
}
}, 500);
我在 Android 中请求权限后尝试执行片段事务 - 但是应用程序不断崩溃。为了避免这种情况,如何更改此代码?
如有任何帮助,我们将不胜感激。
发送电子邮件前检查权限:
private void emailShare() {
if (mayRequestContacts()) {
emailShareImpl();
}
}
检查权限:
private boolean mayRequestContacts() {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
return true;
}
requestPermissions(new String[]{READ_CONTACTS}, REQUEST_READ_CONTACTS);
return false;
}
权限检查结果:
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
if (requestCode == REQUEST_READ_CONTACTS) {
if (grantResults.length == 1 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
emailShareImpl();
}
}
}
如果获得许可则启动片段交易:
private void emailShareImpl() {
FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
EmailReferDialog mEmailReferDialog = new EmailReferDialog();
mEmailReferDialog.setCancelable(true);
mEmailReferDialog.show(fragmentManager, "EMAIL_REFER_DIALOG");
}
LOGCAT:
05-11 09:04:58.859 16113-16113/com.example E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example, PID: 16113
java.lang.RuntimeException: Failure delivering result ResultInfo{who=@android:requestPermissions:, request=65536, result=-1, data=Intent { act=android.content.pm.action.REQUEST_PERMISSIONS (has extras) }} to activity {com.example/com.example.DashboardActivity}: java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
at android.app.ActivityThread.deliverResults(ActivityThread.java:3743)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:3786)
at android.app.ActivityThread.-wrap16(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1412)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5466)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
at android.support.v4.app.FragmentManagerImpl.checkStateLoss(FragmentManager.java:1493)
at android.support.v4.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1511)
at android.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java:638)
at android.support.v4.app.BackStackRecord.commit(BackStackRecord.java:617)
at android.support.v4.app.DialogFragment.show(DialogFragment.java:139)
at com.example.ReferFragment.emailShareImpl(ReferFragment.java:222)
at com.example.ReferFragment.onRequestPermissionsResult(ReferFragment.java:140)
at android.support.v4.app.FragmentActivity.onRequestPermissionsResult(FragmentActivity.java:894)
at android.app.Activity.dispatchRequestPermissionsResult(Activity.java:6582)
at android.app.Activity.dispatchActivityResult(Activity.java:6460)
at android.app.ActivityThread.deliverResults(ActivityThread.java:3739)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:3786)
at android.app.ActivityThread.-wrap16(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1412)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5466)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
发生这种情况是因为 onRequestPermissionresult
在 activity 的 onResume
之前被调用。您可以通过以下方式简单地解决此问题:
private boolean mShareEmail = false;
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
if (requestCode == REQUEST_READ_CONTACTS) {
if (grantResults.length == 1 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
mShareEmail = true;
}
}
}
@Override
public void onResume() {
super.onResume();
if (mShareEmail) {
emailShareImpl();
mShareEmail = false;
}
}
也许下面的提示有点晚了,但不是解决方法,如果你不使用 activity 的状态(例如,权限模型应该是无状态的),你应该使用commitAllowingStateLoss 在片段转换期间。
我通过添加处理程序解决了这个问题:
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
new FolderChooserDialog.Builder(ReportActivity.this)
.tag(REQUEST_CODE_Excel+"")
.show();
}
}, 500);