gpt4 book ai didi

java - Caused by : java. lang.IllegalStateException: 在onSaveInstanceState之后不能执行这个 Action

转载 作者:行者123 更新时间:2023-11-29 06:53:31 25 4
gpt4 key购买 nike

<分区>

我正在尝试获取读取日历的权限,在获取权限后我正在尝试调用另一个 fragment 。我遇到以下异常。这仅在首次启动应用程序时发生。对于下一次启动应用程序,它工作正常。

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();
}
}

}

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com