gpt4 book ai didi

请求运行时权限时出现java.lang.StackOverflowError

转载 作者:行者123 更新时间:2023-12-02 11:08:39 27 4
gpt4 key购买 nike

我创建了一个公共(public)类PermissionManager来管理来自一个地方的所有权限,通常它工作正常,但上传后它显示崩溃分析的错误报告我无法重现,详细信息是下面提到

Fatal Exception: java.lang.StackOverflowError: stack size 8MB
at android.content.res.Resources.getText(Resources.java:308)
at android.content.res.Resources.getString(Resources.java:400)
at android.content.Context.getString(Context.java:409)
at com.mypackage.permission.PermissionManager.checkLocationPermission(PermissionManager.java:46)
at com.mypackage.activities.SplashActivity.onRequestPermissionsResult(SplashActivity.java:98)
at android.app.Activity.requestPermissions(Activity.java:3857)
at android.support.v4.app.ActivityCompat.requestPermissions(ActivityCompat.java:507)
at com.mypackage.permission.PermissionManager.requestPermission(PermissionManager.java:105)
at com.mypackage.permission.PermissionManager.checkPermission(PermissionManager.java:69)
at com.mypackage.permission.PermissionManager.checkLocationPermission(PermissionManager.java:46)
at com.mypackage.activities.SplashActivity.onRequestPermissionsResult(SplashActivity.java:98)
at android.app.Activity.requestPermissions(Activity.java:3857)
at android.support.v4.app.ActivityCompat.requestPermissions(ActivityCompat.java:507)
at com.mypackage.permission.PermissionManager.requestPermission(PermissionManager.java:105)
at com.mypackage.permission.PermissionManager.checkPermission(PermissionManager.java:69)
at com.mypackage.permission.PermissionManager.checkLocationPermission(PermissionManager.java:46)
at com.mypackage.activities.SplashActivity.onRequestPermissionsResult(SplashActivity.java:98)
at android.app.Activity.requestPermissions(Activity.java:3857)
at android.support.v4.app.ActivityCompat.requestPermissions(ActivityCompat.java:507)
at com.mypackage.permission.PermissionManager.requestPermission(PermissionManager.java:105)
at com.mypackage.permission.PermissionManager.checkPermission(PermissionManager.java:69)
at com.mypackage.permission.PermissionManager.checkLocationPermission(PermissionManager.java:46)
at com.mypackage.activities.SplashActivity.onRequestPermissionsResult(SplashActivity.java:98)
at android.app.Activity.requestPermissions(Activity.java:3857)
at android.support.v4.app.ActivityCompat.requestPermissions(ActivityCompat.java:507)
at com.mypackage.permission.PermissionManager.requestPermission(PermissionManager.java:105)
at com.mypackage.permission.PermissionManager.checkPermission(PermissionManager.java:69)
at com.mypackage.permission.PermissionManager.checkLocationPermission(PermissionManager.java:46)
at com.mypackage.activities.SplashActivity.onRequestPermissionsResult(SplashActivity.java:98)
at android.app.Activity.requestPermissions(Activity.java:3857)
at android.support.v4.app.ActivityCompat.requestPermissions(ActivityCompat.java:507)
at com.mypackage.permission.PermissionManager.requestPermission(PermissionManager.java:105)
at com.mypackage.permission.PermissionManager.checkPermission(PermissionManager.java:69)
at com.mypackage.permission.PermissionManager.checkLocationPermission(PermissionManager.java:46)
at com.mypackage.activities.SplashActivity.onRequestPermissionsResult(SplashActivity.java:98)
at android.app.Activity.requestPermissions(Activity.java:3857)
at android.support.v4.app.ActivityCompat.requestPermissions(ActivityCompat.java:507)
at com.mypackage.permission.PermissionManager.requestPermission(PermissionManager.java:105)
at com.mypackage.permission.PermissionManager.checkPermission(PermissionManager.java:69)
at com.mypackage.permission.PermissionManager.checkLocationPermission(PermissionManager.java:46)
at com.mypackage.activities.SplashActivity.onRequestPermissionsResult(SplashActivity.java:98)
at android.app.Activity.requestPermissions(Activity.java:3857)
at android.support.v4.app.ActivityCompat.requestPermissions(ActivityCompat.java:507)
at com.mypackage.permission.PermissionManager.requestPermission(PermissionManager.java:105)
at com.mypackage.permission.PermissionManager.checkPermission(PermissionManager.java:69)
at com.mypackage.permission.PermissionManager.checkLocationPermission(PermissionManager.java:46)
at com.mypackage.activities.SplashActivity.onRequestPermissionsResult(SplashActivity.java:98)
at android.app.Activity.requestPermissions(Activity.java:3857)
at android.support.v4.app.ActivityCompat.requestPermissions(ActivityCompat.java:507)
at com.mypackage.permission.PermissionManager.requestPermission(PermissionManager.java:105)
at com.mypackage.permission.PermissionManager.checkPermission(PermissionManager.java:69)
at com.mypackage.permission.PermissionManager.checkLocationPermission(PermissionManager.java:46)
at com.mypackage.activities.SplashActivity.onRequestPermissionsResult(SplashActivity.java:98)
at android.app.Activity.requestPermissions(Activity.java:3857)
at android.support.v4.app.ActivityCompat.requestPermissions(ActivityCompat.java:507)
at com.mypackage.permission.PermissionManager.requestPermission(PermissionManager.java:105)
at com.mypackage.permission.PermissionManager.checkPermission(PermissionManager.java:69)
at com.mypackage.permission.PermissionManager.checkLocationPermission(PermissionManager.java:46)
at com.mypackage.activities.SplashActivity.onRequestPermissionsResult(SplashActivity.java:98)
at android.app.Activity.requestPermissions(Activity.java:3857)
at android.support.v4.app.ActivityCompat.requestPermissions(ActivityCompat.java:507)
at com.mypackage.permission.PermissionManager.requestPermission(PermissionManager.java:105)
at com.mypackage.permission.PermissionManager.checkPermission(PermissionManager.java:69)
at com.mypackage.permission.PermissionManager.checkLocationPermission(PermissionManager.java:46)
at com.mypackage.activities.SplashActivity.onCreate(SplashActivity.java:53)
at android.app.Activity.performCreate(Activity.java:6285)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1108)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.access$900(ActivityThread.java:150)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:171)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Method.java)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

SplashActivity.java

 @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

if (PermissionManager.checkLocationPermission(this)) {

// do the task
}
}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode) {
case PermissionManager.MY_PERMISSIONS_REQUEST_LOCATION_ACCESS: {
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// do the task
} else {
// Toast.makeText(getApplicationContext(), "For find better result,enable GPS permission from setting", Toast.LENGTH_LONG).show();
if (!PermissionManager.MY_REQUESTED_DIALOG) {
PermissionManager.checkLocationPermission(this);
} else {
startNextActivity(0);
}
}


}
}
}

PermissionManager.java

public class PermissionManager {

public static final int MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE = 101;
public static final int MY_PERMISSIONS_REQUEST_LOCATION_ACCESS = 102;
public static final int MY_PERMISSIONS_REQUEST_CAMERA = 103;
public static final int MY_PERMISSIONS_REQUEST_CALL = 104;
public static final int MY_PERMISSIONS_REQUEST_AUDIO = 105;

public static boolean MY_REQUESTED_DIALOG = false;


private static String[] storagePermission = new String[]{Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE};
private static String[] cameraPermission = new String[]{Manifest.permission.CAMERA};
private static String[] locationPermission = new String[]{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION};
private static String[] phoneCallPermission = new String[]{Manifest.permission.CALL_PHONE};
private static String[] recordAudioCallPermission = new String[]{Manifest.permission.RECORD_AUDIO};


public static boolean checkPhoneCallPermission(final Activity context) {
return checkPermission(context, phoneCallPermission, "Permission necessary", "Allow call permission", MY_PERMISSIONS_REQUEST_CALL);
}


public static boolean checkStoragePermission(final Activity context) {
return checkPermission(context, storagePermission, "", context.getString(R.string.storage_permission_rejected), MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE);
}

public static boolean checkLocationPermission(final Activity context) {
return checkPermission(context, locationPermission, "", context.getString(R.string.location_permission_rejected), MY_PERMISSIONS_REQUEST_LOCATION_ACCESS);
}

public static boolean checkRecordAudioPermission(final Activity context) {
return checkPermission(context, recordAudioCallPermission, "", context.getString(R.string.audio_permission_rejected), MY_PERMISSIONS_REQUEST_AUDIO);
}


/*
*
* Check the permission
*
* */

private static boolean checkPermission(final Activity context, String[] permission, String messageTitle, String messageDetails, int requestCode) {

MY_REQUESTED_DIALOG = false;
if (checkAPIVersion()) {
if (checkPermissionStatus(context, permission)) {
if (shouldShowRequestPermission(context, permission)) {
MY_REQUESTED_DIALOG = true;
showDialogForPermission(context, messageTitle, messageDetails, permission, requestCode);
} else {
requestPermission(context, permission, requestCode);
}
return false;
} else {
return true;
}
}
return true;

}

/*
*
* Show dialog for why ,this permission is needed
*
* */
private static void showDialogForPermission(final Activity context, String title, String message, final String[] requestedPermission, final int requestCode) {
AlertDialog.Builder alertBuilder = new AlertDialog.Builder(context);
alertBuilder.setCancelable(false);
alertBuilder.setTitle(title);
alertBuilder.setMessage(message);
alertBuilder.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
requestPermission(context, requestedPermission, requestCode);
}
});
AlertDialog alert = alertBuilder.create();
alert.show();
}

/*
*
* Request for permission
*
* */
private static void requestPermission(Activity context, String[] requestedPermission, int requestCode) {
ActivityCompat.requestPermissions(context, requestedPermission, requestCode);
}

/*
*
* Check need to display dialog for permission access
* executed when first time user decline the permission
*
* */
private static boolean shouldShowRequestPermission(Activity context, String[] permissionCheck) {
for (String permission : permissionCheck) {
if (ActivityCompat.shouldShowRequestPermissionRationale(context, permission))
return true;
}

return false;
}

/*
*
* Check list of permission is granted or not
* if not requested for permission
*
* */
private static boolean checkPermissionStatus(Activity context, String[] permissionCheck) {
for (String permission : permissionCheck) {
if (ContextCompat.checkSelfPermission(context, permission) != PackageManager.PERMISSION_GRANTED)
return true;
}
return false;
}

/*
*
* Check API version
* if it's >=M need to check run time permission
* */
private static boolean checkAPIVersion() {
return Build.VERSION.SDK_INT >= Build.VERSION_CODES.M;
}

}

谁能帮忙解决这个问题吗?

最佳答案

您遇到了无限循环,我猜这是由于某人拒绝位置权限并选中“从不询问agan”复选框造成的:

enter image description here

这将自动拒绝任何进一步的位置权限请求。在您的情况下,这成为一个大问题,因为 PermissionManager::checkPermission() 使用 showDialogForPermission() 提示权限对话框,该对话框会自动拒绝,进而调用 onRequestPermissionsResult().

if (shouldShowRequestPermission(context, permission)) {
MY_REQUESTED_DIALOG = true;
showDialogForPermission(context, messageTitle, messageDetails, permission, requestCode);
} else {
/* THIS LINE IS STARTING THE INFINITE LOOP */
requestPermission(context, permission, requestCode);
}

因为 shouldShowRequestPermission(context,permission) 返回 falseMY_REQUESTED_DIALOG 永远不会设置为 true 并且流程一遍又一遍地循环。我的建议是,如果 shouldShowRequestPermission() == false 并且您的权限在 onRequestPermissionsResult() 中被拒绝,则中断权限流程。

if (shouldShowRequestPermission(context, permission)) {
MY_REQUESTED_DIALOG = true;
showDialogForPermission(context, messageTitle, messageDetails, permission, requestCode);
} else {
// Gracefully handle the fact that Location permission will never be granted
}

关于请求运行时权限时出现java.lang.StackOverflowError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50738945/

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