2

Some users in my app get this error:

Caused by: java.lang.ArrayIndexOutOfBoundsException: at com.file.gt.MainActivity.onRequestPermissionsResult (MainActivity.java:234) at android.app.Activity.requestPermissions (Activity.java:5323) at com.file.gt.MainActivity.onCreate (MainActivity.java:85) at android.app.Activity.performCreate (Activity.java:8114) at android.app.Activity.performCreate (Activity.java:8098) at android.app.Instrumentation.callActivityOnCreate (Instrumentation.java:1309) at android.app.ActivityThread.performLaunchActivity (ActivityThread.java:3480)

It occurs only sometimes, with some users.

I have this in my app:

private final String [] permissions = {
        "android.permission.RECORD_AUDIO",
        "android.permission.READ_PHONE_STATE"
};

        int requestCode = 200;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (checkSelfPermission(permissions[0]) == PackageManager.PERMISSION_DENIED ||
                    checkSelfPermission(permissions[1]) == PackageManager.PERMISSION_DENIED) {
                requestPermissions(permissions, requestCode);
            }
        }

and this:

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    if(grantResults[0] == PackageManager.PERMISSION_GRANTED
    && grantResults[1] == PackageManager.PERMISSION_GRANTED) {
        recreate(); // 234
    }
    else {
        AlertDialog alertDialog = new AlertDialog.Builder(MainActivity.this).create();
        alertDialog.setTitle(getString(R.string.alert_title));
        alertDialog.setMessage(getString(R.string.alert_text));
        alertDialog.setButton(AlertDialog.BUTTON_NEUTRAL, "OK",
                (dialog, which) -> dialog.dismiss());
        alertDialog.show();
    }
}

Any ideas what is wrong and what I'm doing wrong? Thanks.

RGS
  • 4,062
  • 4
  • 31
  • 67
  • 1
    u know there is a tool called debugger. You can even put conditions in the debugger in modern IDEs like Intellij to only stop at a breakpoint if condition (like out of bounds) applies. You should seriously go and explore debugging, because that is an essential skill of a developer – J Asgarov Jan 23 '22 at 09:10
  • @JAsgarov thanks, I will learn. But I know where the problem is, just don't know what is missing on my onRequestPermissionsResult. – RGS Jan 23 '22 at 09:22

2 Answers2

4

When you get an ArrayIndexOutOfBoundsException it means you are accessing an array index outside the array size. Your stack trace says it happens in onRequestPermissionsResult, and the only array you access there is grantResults.

What can sometimes happen is that grantResults is empty in onRequestPermissionsResult if the user declines the permissions, so you should check its size before accessing its members

if(grantResults.length == 2 &&
   grantResults[0] == PackageManager.PERMISSION_GRANTED &&
   grantResults[1] == PackageManager.PERMISSION_GRANTED) {
    recreate();
}
else {
    // handle permissions not granted
}

Alternately, if you use the new registerForActivityResult approach you can get around this entirely. Have a look here for more info, or here for an example use.

Tyler V
  • 9,694
  • 3
  • 26
  • 52
0
private final String [] permissions = {
android.Manifest.permission.RECORD_AUDIO, 
android.Manifest.permission. READ_PHONE_STATE,
};
int requestCode = 200;

public static boolean hasPermissions(Context context, String... permissions) {
if (context != null && permissions != null) {
    for (String permission : permissions) {
        if (ActivityCompat.checkSelfPermission(context, permission) != PackageManager.PERMISSION_GRANTED) {
            return false;
        }
    }
}
return true;}

//user this code to check

if (!hasPermissions(this, PERMISSIONS)) {
   ActivityCompat.requestPermissions(this, PERMISSIONS, PERMISSION_ALL);
}else{
   recreate(); 
}