我正在尝试获取读取日历的权限,获取权限后我试图调用另一个片段。我遇到了以下异常。这只发生在第一次启动应用程序时。对于下一个启动应用程序工作正常。
5488-5488/com.mindtree.calenderevents E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.mindtree.calenderevents, PID: 5488
java.lang.RuntimeException: Failure delivering result ResultInfo{who=@android:requestPermissions:, request=2, result=-1, data=Intent { act=android.content.pm.action.REQUEST_PERMISSIONS (has extras) }} to activity {com.mindtree.calenderevents/com.mindtree.calenderevents.MainActivity}: java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
at android.app.ActivityThread.deliverResults(ActivityThread.java:4998)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:5041)
at android.app.ActivityThread.access$1600(ActivityThread.java:229)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1875)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:7325)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
Caused by: java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
at android.support.v4.app.FragmentManagerImpl.checkStateLoss(FragmentManager.java:1533)
at android.support.v4.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1551)
at android.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java:696)
at android.support.v4.app.BackStackRecord.commit(BackStackRecord.java:662)
at com.mindtree.calenderevents.MainActivity.onRequestPermissionsResult(MainActivity.java:59)
at android.app.Activity.dispatchRequestPermissionsResult(Activity.java:7291)
at android.app.Activity.dispatchActivityResult(Activity.java:7169)
at android.app.ActivityThread.deliverResults(ActivityThread.java:4994)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:5041)
at android.app.ActivityThread.access$1600(ActivityThread.java:229)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1875)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:7325)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
这是我的代码
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TopSectionFragment fragment = TopSectionFragment.newInstance();
getSupportFragmentManager().beginTransaction().replace(R.id.fragment, fragment)
.commit();
}
@Override
public void getEvents() {
Log.i("mainacteventmethod ","mainacteventmethod");
//TODO: implement method to fetch events from calendar and add as fragment arguments
getPermissionToReadCalender();
}
@Override
public void onRequestPermissionsResult(int requestCode,
@NonNull String permissions[], @NonNull int[] grantResults) {
switch (requestCode) {
case READ_CALENDER: {
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// permission was granted, yay! Do the
// contacts-related task you need to do.
RuntimeData.setsCalendarData(getCalender(null));
if (RuntimeData.getsCalendarData() != null) {
BottomSectionFragment bottomFragment = new BottomSectionFragment();
getSupportFragmentManager().beginTransaction().replace(R.id.fragment2,
bottomFragment).commit();
}
} else {
// permission denied, boo! Disable the
// functionality that depends on this permission.
}
return;
}
// other 'case' lines to check for other
// permissions this app might request
}
}
private void getPermissionToReadCalender() {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_CALENDAR) != PackageManager.PERMISSION_GRANTED) {
if (shouldShowRequestPermissionRationale(
Manifest.permission.READ_CALENDAR)) {
}
requestPermissions(new String[]{Manifest.permission.READ_CALENDAR},
READ_CALENDER);
}else{
RuntimeData.setsCalendarData(getCalender(null));
if (RuntimeData.getsCalendarData() != null) {
BottomSectionFragment bottomFragment = new BottomSectionFragment();
getSupportFragmentManager().beginTransaction().replace(R.id.fragment2,
bottomFragment).commit();
}
}
}
答案 0 :(得分:5)
如果在片段活动的onSaveInstanceState()
被调用后尝试执行片段转换,则会发生此异常。
要测试这是否是这种情况,请覆盖onSaveInstanceState(),以便它不会调用super方法。
@Override
protected void onSaveInstanceState(Bundle outState) {
//No call for super().
}
如果这样可以解决问题,而不是片段管理器上的commit()
,请使用commitAllowingStateLoss();