gpt4 book ai didi

android - IllegalStateException - 调用 DialogFragment.show() 方法时无法在 onSaveInstanceState 之后执行此操作

转载 作者:行者123 更新时间:2023-11-29 15:02:32 25 4
gpt4 key购买 nike

我正在开发一个关于如何请求 ACCESS_FINE_LOCATION 权限的 Android 6.0 应用示例,如下图所示。

enter image description here

选择拒绝选项时,我想向用户显示一个自定义对话框,但会引发以下异常(exception):

IllegalStateException - 在 onSaveInstanceState 之后无法执行此操作

调用方法时抛出:

grantPermissionDialog.show(getSupportFragmentManager(),"GrantPermissionDialog");

在 onRequestPermissionsResult 方法中

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if(requestCode == REQUEST_ACCESS_FINE_LOCATION){
if(grantResults.length == 1 && grantResults[0] == PackageManager.PERMISSION_GRANTED){
Toast.makeText(this, "Location permission granted", Toast.LENGTH_LONG).show();
}else{
grantPermissionDialog.show(getSupportFragmentManager(),"GrantPermissionDialog");
}
}
}

我想这是因为调用了 getSupportFragmentManager() 方法。这是我的 GrantPermissionDialog 代码。

public class GrantPermissionDialog extends DialogFragment {

private int statusCode;
private String message="El permiso de ubicación es necesario para que esta aplicación funcione.";
private String positiveButtonText="Salir";
private String negativeButtonText="Solicitar permiso";
public static int EXIT_APPLICATION=0;
public static int REQUEST_PERMISSION=1;

@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setMessage(message);
builder.setPositiveButton(positiveButtonText, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
statusCode = EXIT_APPLICATION;
}
});
builder.setNegativeButton(negativeButtonText, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
statusCode = REQUEST_PERMISSION;
}
});
return builder.create();
}


public int getStatusCode() {
return statusCode;
}

关于如何解决这个问题有什么想法吗?

最佳答案

这是一个已知的场景,并且得到了很好的解释 here .根据那篇文章,这是造成

because you attempted to commit a FragmentTransaction after the activity’s state had been saved

您应该将 show() 对话框代码移动到“onPostResume()”并使用一个标志,如 here 所述

private boolean mRequestPermissionsWithResult = false;    

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if(requestCode == REQUEST_ACCESS_FINE_LOCATION){
if(grantResults.length == 1 && grantResults[0] == PackageManager.PERMISSION_GRANTED){
Toast.makeText(this, "Location permission granted", Toast.LENGTH_LONG).show();
}else{
mRequestPermissionsWithResult = true;
}
}
}

@Override
protected void onPostResume() {
super.onPostResume();
if (mRequestPermissionsWithResult) {
grantPermissionDialog.show(getSupportFragmentManager(),"GrantPermissionDialog");
}
mRequestPermissionsWithResult = false;
}

关于android - IllegalStateException - 调用 DialogFragment.show() 方法时无法在 onSaveInstanceState 之后执行此操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42262031/

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