Requesting camera permissions results in NullPointerException in String.equals
I'm trying to request camera permissions to use Google OCR in my Android app, but on most devices it crashes on install.
I've read similar posts that say this is caused by a null context being passed into ContextCompat.checkSelfPermission()
, but I can't see how the context would be null.
Here is my code:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
StrictMode.setVmPolicy(builder.build());
surfaceView = findViewById(R.id.surfaceView);
if (checkForPermissions(Manifest.permission.CAMERA,
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.INTERNET)) {
surfaceView.getHolder().addCallback(new SurfaceCallback());
}
}
...
private boolean checkForPermissions(String ... permissions) {
List<String> permissionsToRequest = new ArrayList<>(Arrays.asList(permissions));
for (Iterator<String> it = permissionsToRequest.iterator(); it.hasNext();) {
if (ContextCompat.checkSelfPermission(this, it.next()) == PackageManager.PERMISSION_GRANTED) {
it.remove();
}
}
if (permissionsToRequest.isEmpty()) {
return true;
}
ActivityCompat.requestPermissions(MainActivity.this, permissionsToRequest.toArray(permissions), 0);
return false;
}
@Override
public void onRequestPermissionsResult(int requestCode,
@NonNull String permissions,
@NonNull int results) {
if (requestCode != 0) {
return;
}
for (int i = 0; i < results.length; i++) {
if (results[i] != PackageManager.PERMISSION_GRANTED && permissions[i] != null) {
AlertDialog alertDialog = new AlertDialog.Builder(MainActivity.this).create();
alertDialog.setTitle("Permissions needed");
alertDialog.setMessage("This app requires camera permissions to function properly.");
alertDialog.setButton(AlertDialog.BUTTON_POSITIVE, "OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
ActivityCompat.requestPermissions(MainActivity.this,
new String {
Manifest.permission.CAMERA,
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.INTERNET
},
0);
}
});
alertDialog.show();
return;
}
}
surfaceView.getHolder().addCallback(new SurfaceCallback());
}
SurfaceCallback
implements SurfaceHolder.Callback
and that's where the camera is actually initialized.
This is the exception:
4606/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.google.android.packageinstaller, PID: 4606
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.google.android.packageinstaller/com.android.packageinstaller.permission.ui.GrantPermissionsActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference
at com.android.packageinstaller.permission.ui.GrantPermissionsActivity.computePermissionGrantState(GrantPermissionsActivity.java:314)
at com.android.packageinstaller.permission.ui.GrantPermissionsActivity.updateDefaultResults(GrantPermissionsActivity.java:363)
at com.android.packageinstaller.permission.ui.GrantPermissionsActivity.onCreate(GrantPermissionsActivity.java:122)
at android.app.Activity.performCreate(Activity.java:6662)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
java android
add a comment |
I'm trying to request camera permissions to use Google OCR in my Android app, but on most devices it crashes on install.
I've read similar posts that say this is caused by a null context being passed into ContextCompat.checkSelfPermission()
, but I can't see how the context would be null.
Here is my code:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
StrictMode.setVmPolicy(builder.build());
surfaceView = findViewById(R.id.surfaceView);
if (checkForPermissions(Manifest.permission.CAMERA,
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.INTERNET)) {
surfaceView.getHolder().addCallback(new SurfaceCallback());
}
}
...
private boolean checkForPermissions(String ... permissions) {
List<String> permissionsToRequest = new ArrayList<>(Arrays.asList(permissions));
for (Iterator<String> it = permissionsToRequest.iterator(); it.hasNext();) {
if (ContextCompat.checkSelfPermission(this, it.next()) == PackageManager.PERMISSION_GRANTED) {
it.remove();
}
}
if (permissionsToRequest.isEmpty()) {
return true;
}
ActivityCompat.requestPermissions(MainActivity.this, permissionsToRequest.toArray(permissions), 0);
return false;
}
@Override
public void onRequestPermissionsResult(int requestCode,
@NonNull String permissions,
@NonNull int results) {
if (requestCode != 0) {
return;
}
for (int i = 0; i < results.length; i++) {
if (results[i] != PackageManager.PERMISSION_GRANTED && permissions[i] != null) {
AlertDialog alertDialog = new AlertDialog.Builder(MainActivity.this).create();
alertDialog.setTitle("Permissions needed");
alertDialog.setMessage("This app requires camera permissions to function properly.");
alertDialog.setButton(AlertDialog.BUTTON_POSITIVE, "OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
ActivityCompat.requestPermissions(MainActivity.this,
new String {
Manifest.permission.CAMERA,
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.INTERNET
},
0);
}
});
alertDialog.show();
return;
}
}
surfaceView.getHolder().addCallback(new SurfaceCallback());
}
SurfaceCallback
implements SurfaceHolder.Callback
and that's where the camera is actually initialized.
This is the exception:
4606/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.google.android.packageinstaller, PID: 4606
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.google.android.packageinstaller/com.android.packageinstaller.permission.ui.GrantPermissionsActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference
at com.android.packageinstaller.permission.ui.GrantPermissionsActivity.computePermissionGrantState(GrantPermissionsActivity.java:314)
at com.android.packageinstaller.permission.ui.GrantPermissionsActivity.updateDefaultResults(GrantPermissionsActivity.java:363)
at com.android.packageinstaller.permission.ui.GrantPermissionsActivity.onCreate(GrantPermissionsActivity.java:122)
at android.app.Activity.performCreate(Activity.java:6662)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
java android
2
Your problem is inside of acomputePermissionGrantState()
, which is not shown in your question's source code.
– CommonsWare
Dec 31 '18 at 23:49
1
@CommonsWare Forgive my ignorance, but I'm not even sure how to get to that code. By that I mean it's Android's code that I can't just click on from logcat.
– C. Swainston
Dec 31 '18 at 23:58
Sorry, I was misreading the stack trace, focusing too much on the class and not enough on the package. Let me putter through the source code and see if I can identify what might be wrong.
– CommonsWare
Jan 1 at 0:13
One thing that you can fix: get rid ofManifest.permission.INTERNET
from yourcheckForPermissions()
call, as that is not adangerous
permission.
– CommonsWare
Jan 1 at 0:15
1
The issue is with thetoArray
incheckForPermissions
- see List doc: If the list fits in the specified array with room to spare (i.e., the array has more elements than the list), the element in the array immediately following the end of the list is set to null. Most likely it is a case where you pruned the original list by at least 1.
– Andy
Jan 1 at 0:19
add a comment |
I'm trying to request camera permissions to use Google OCR in my Android app, but on most devices it crashes on install.
I've read similar posts that say this is caused by a null context being passed into ContextCompat.checkSelfPermission()
, but I can't see how the context would be null.
Here is my code:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
StrictMode.setVmPolicy(builder.build());
surfaceView = findViewById(R.id.surfaceView);
if (checkForPermissions(Manifest.permission.CAMERA,
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.INTERNET)) {
surfaceView.getHolder().addCallback(new SurfaceCallback());
}
}
...
private boolean checkForPermissions(String ... permissions) {
List<String> permissionsToRequest = new ArrayList<>(Arrays.asList(permissions));
for (Iterator<String> it = permissionsToRequest.iterator(); it.hasNext();) {
if (ContextCompat.checkSelfPermission(this, it.next()) == PackageManager.PERMISSION_GRANTED) {
it.remove();
}
}
if (permissionsToRequest.isEmpty()) {
return true;
}
ActivityCompat.requestPermissions(MainActivity.this, permissionsToRequest.toArray(permissions), 0);
return false;
}
@Override
public void onRequestPermissionsResult(int requestCode,
@NonNull String permissions,
@NonNull int results) {
if (requestCode != 0) {
return;
}
for (int i = 0; i < results.length; i++) {
if (results[i] != PackageManager.PERMISSION_GRANTED && permissions[i] != null) {
AlertDialog alertDialog = new AlertDialog.Builder(MainActivity.this).create();
alertDialog.setTitle("Permissions needed");
alertDialog.setMessage("This app requires camera permissions to function properly.");
alertDialog.setButton(AlertDialog.BUTTON_POSITIVE, "OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
ActivityCompat.requestPermissions(MainActivity.this,
new String {
Manifest.permission.CAMERA,
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.INTERNET
},
0);
}
});
alertDialog.show();
return;
}
}
surfaceView.getHolder().addCallback(new SurfaceCallback());
}
SurfaceCallback
implements SurfaceHolder.Callback
and that's where the camera is actually initialized.
This is the exception:
4606/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.google.android.packageinstaller, PID: 4606
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.google.android.packageinstaller/com.android.packageinstaller.permission.ui.GrantPermissionsActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference
at com.android.packageinstaller.permission.ui.GrantPermissionsActivity.computePermissionGrantState(GrantPermissionsActivity.java:314)
at com.android.packageinstaller.permission.ui.GrantPermissionsActivity.updateDefaultResults(GrantPermissionsActivity.java:363)
at com.android.packageinstaller.permission.ui.GrantPermissionsActivity.onCreate(GrantPermissionsActivity.java:122)
at android.app.Activity.performCreate(Activity.java:6662)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
java android
I'm trying to request camera permissions to use Google OCR in my Android app, but on most devices it crashes on install.
I've read similar posts that say this is caused by a null context being passed into ContextCompat.checkSelfPermission()
, but I can't see how the context would be null.
Here is my code:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
StrictMode.setVmPolicy(builder.build());
surfaceView = findViewById(R.id.surfaceView);
if (checkForPermissions(Manifest.permission.CAMERA,
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.INTERNET)) {
surfaceView.getHolder().addCallback(new SurfaceCallback());
}
}
...
private boolean checkForPermissions(String ... permissions) {
List<String> permissionsToRequest = new ArrayList<>(Arrays.asList(permissions));
for (Iterator<String> it = permissionsToRequest.iterator(); it.hasNext();) {
if (ContextCompat.checkSelfPermission(this, it.next()) == PackageManager.PERMISSION_GRANTED) {
it.remove();
}
}
if (permissionsToRequest.isEmpty()) {
return true;
}
ActivityCompat.requestPermissions(MainActivity.this, permissionsToRequest.toArray(permissions), 0);
return false;
}
@Override
public void onRequestPermissionsResult(int requestCode,
@NonNull String permissions,
@NonNull int results) {
if (requestCode != 0) {
return;
}
for (int i = 0; i < results.length; i++) {
if (results[i] != PackageManager.PERMISSION_GRANTED && permissions[i] != null) {
AlertDialog alertDialog = new AlertDialog.Builder(MainActivity.this).create();
alertDialog.setTitle("Permissions needed");
alertDialog.setMessage("This app requires camera permissions to function properly.");
alertDialog.setButton(AlertDialog.BUTTON_POSITIVE, "OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
ActivityCompat.requestPermissions(MainActivity.this,
new String {
Manifest.permission.CAMERA,
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.INTERNET
},
0);
}
});
alertDialog.show();
return;
}
}
surfaceView.getHolder().addCallback(new SurfaceCallback());
}
SurfaceCallback
implements SurfaceHolder.Callback
and that's where the camera is actually initialized.
This is the exception:
4606/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.google.android.packageinstaller, PID: 4606
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.google.android.packageinstaller/com.android.packageinstaller.permission.ui.GrantPermissionsActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference
at com.android.packageinstaller.permission.ui.GrantPermissionsActivity.computePermissionGrantState(GrantPermissionsActivity.java:314)
at com.android.packageinstaller.permission.ui.GrantPermissionsActivity.updateDefaultResults(GrantPermissionsActivity.java:363)
at com.android.packageinstaller.permission.ui.GrantPermissionsActivity.onCreate(GrantPermissionsActivity.java:122)
at android.app.Activity.performCreate(Activity.java:6662)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
java android
java android
edited Jan 1 at 4:17
Melchizedek
6261120
6261120
asked Dec 31 '18 at 23:45
C. SwainstonC. Swainston
134
134
2
Your problem is inside of acomputePermissionGrantState()
, which is not shown in your question's source code.
– CommonsWare
Dec 31 '18 at 23:49
1
@CommonsWare Forgive my ignorance, but I'm not even sure how to get to that code. By that I mean it's Android's code that I can't just click on from logcat.
– C. Swainston
Dec 31 '18 at 23:58
Sorry, I was misreading the stack trace, focusing too much on the class and not enough on the package. Let me putter through the source code and see if I can identify what might be wrong.
– CommonsWare
Jan 1 at 0:13
One thing that you can fix: get rid ofManifest.permission.INTERNET
from yourcheckForPermissions()
call, as that is not adangerous
permission.
– CommonsWare
Jan 1 at 0:15
1
The issue is with thetoArray
incheckForPermissions
- see List doc: If the list fits in the specified array with room to spare (i.e., the array has more elements than the list), the element in the array immediately following the end of the list is set to null. Most likely it is a case where you pruned the original list by at least 1.
– Andy
Jan 1 at 0:19
add a comment |
2
Your problem is inside of acomputePermissionGrantState()
, which is not shown in your question's source code.
– CommonsWare
Dec 31 '18 at 23:49
1
@CommonsWare Forgive my ignorance, but I'm not even sure how to get to that code. By that I mean it's Android's code that I can't just click on from logcat.
– C. Swainston
Dec 31 '18 at 23:58
Sorry, I was misreading the stack trace, focusing too much on the class and not enough on the package. Let me putter through the source code and see if I can identify what might be wrong.
– CommonsWare
Jan 1 at 0:13
One thing that you can fix: get rid ofManifest.permission.INTERNET
from yourcheckForPermissions()
call, as that is not adangerous
permission.
– CommonsWare
Jan 1 at 0:15
1
The issue is with thetoArray
incheckForPermissions
- see List doc: If the list fits in the specified array with room to spare (i.e., the array has more elements than the list), the element in the array immediately following the end of the list is set to null. Most likely it is a case where you pruned the original list by at least 1.
– Andy
Jan 1 at 0:19
2
2
Your problem is inside of a
computePermissionGrantState()
, which is not shown in your question's source code.– CommonsWare
Dec 31 '18 at 23:49
Your problem is inside of a
computePermissionGrantState()
, which is not shown in your question's source code.– CommonsWare
Dec 31 '18 at 23:49
1
1
@CommonsWare Forgive my ignorance, but I'm not even sure how to get to that code. By that I mean it's Android's code that I can't just click on from logcat.
– C. Swainston
Dec 31 '18 at 23:58
@CommonsWare Forgive my ignorance, but I'm not even sure how to get to that code. By that I mean it's Android's code that I can't just click on from logcat.
– C. Swainston
Dec 31 '18 at 23:58
Sorry, I was misreading the stack trace, focusing too much on the class and not enough on the package. Let me putter through the source code and see if I can identify what might be wrong.
– CommonsWare
Jan 1 at 0:13
Sorry, I was misreading the stack trace, focusing too much on the class and not enough on the package. Let me putter through the source code and see if I can identify what might be wrong.
– CommonsWare
Jan 1 at 0:13
One thing that you can fix: get rid of
Manifest.permission.INTERNET
from your checkForPermissions()
call, as that is not a dangerous
permission.– CommonsWare
Jan 1 at 0:15
One thing that you can fix: get rid of
Manifest.permission.INTERNET
from your checkForPermissions()
call, as that is not a dangerous
permission.– CommonsWare
Jan 1 at 0:15
1
1
The issue is with the
toArray
in checkForPermissions
- see List doc: If the list fits in the specified array with room to spare (i.e., the array has more elements than the list), the element in the array immediately following the end of the list is set to null. Most likely it is a case where you pruned the original list by at least 1.– Andy
Jan 1 at 0:19
The issue is with the
toArray
in checkForPermissions
- see List doc: If the list fits in the specified array with room to spare (i.e., the array has more elements than the list), the element in the array immediately following the end of the list is set to null. Most likely it is a case where you pruned the original list by at least 1.– Andy
Jan 1 at 0:19
add a comment |
1 Answer
1
active
oldest
votes
By looking at the source code, my best guess is that you are winding up with null
as one of the entries in the String
that you are passing to ActivityCompat.requestPermissions()
.
That might be due to something with how you are converting between arrays and lists. In particular, permissionsToRequest.toArray(permissions)
is kinda scary, as you are trying to reuse a String
that is passed in as a parameter. Plus, as Andy pointed out, toArray()
sets unused array elements to null
.
I'd allocate a fresh String
, as I do here:
private boolean hasPermission(String perm) {
return(ContextCompat.checkSelfPermission(this, perm)==
PackageManager.PERMISSION_GRANTED);
}
private String netPermissions(String wanted) {
ArrayList<String> result=new ArrayList<String>();
for (String perm : wanted) {
if (!hasPermission(perm)) {
result.add(perm);
}
}
return(result.toArray(new String[result.size()]));
}
Thank you! This fixed the problem. I would not have thought to look there. Also, thanks for the heads up on theINTERNET
permission.
– C. Swainston
Jan 1 at 0:38
1
@C.Swainston: Ideally,ActivityCompat.requestPermissions()
would do some sanity-checking on the inputs, so you'd fail with a better error message. I filed a feature request for that. Glad to hear that you got it working!
– CommonsWare
Jan 1 at 0:55
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53992172%2frequesting-camera-permissions-results-in-nullpointerexception-in-string-equals%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
By looking at the source code, my best guess is that you are winding up with null
as one of the entries in the String
that you are passing to ActivityCompat.requestPermissions()
.
That might be due to something with how you are converting between arrays and lists. In particular, permissionsToRequest.toArray(permissions)
is kinda scary, as you are trying to reuse a String
that is passed in as a parameter. Plus, as Andy pointed out, toArray()
sets unused array elements to null
.
I'd allocate a fresh String
, as I do here:
private boolean hasPermission(String perm) {
return(ContextCompat.checkSelfPermission(this, perm)==
PackageManager.PERMISSION_GRANTED);
}
private String netPermissions(String wanted) {
ArrayList<String> result=new ArrayList<String>();
for (String perm : wanted) {
if (!hasPermission(perm)) {
result.add(perm);
}
}
return(result.toArray(new String[result.size()]));
}
Thank you! This fixed the problem. I would not have thought to look there. Also, thanks for the heads up on theINTERNET
permission.
– C. Swainston
Jan 1 at 0:38
1
@C.Swainston: Ideally,ActivityCompat.requestPermissions()
would do some sanity-checking on the inputs, so you'd fail with a better error message. I filed a feature request for that. Glad to hear that you got it working!
– CommonsWare
Jan 1 at 0:55
add a comment |
By looking at the source code, my best guess is that you are winding up with null
as one of the entries in the String
that you are passing to ActivityCompat.requestPermissions()
.
That might be due to something with how you are converting between arrays and lists. In particular, permissionsToRequest.toArray(permissions)
is kinda scary, as you are trying to reuse a String
that is passed in as a parameter. Plus, as Andy pointed out, toArray()
sets unused array elements to null
.
I'd allocate a fresh String
, as I do here:
private boolean hasPermission(String perm) {
return(ContextCompat.checkSelfPermission(this, perm)==
PackageManager.PERMISSION_GRANTED);
}
private String netPermissions(String wanted) {
ArrayList<String> result=new ArrayList<String>();
for (String perm : wanted) {
if (!hasPermission(perm)) {
result.add(perm);
}
}
return(result.toArray(new String[result.size()]));
}
Thank you! This fixed the problem. I would not have thought to look there. Also, thanks for the heads up on theINTERNET
permission.
– C. Swainston
Jan 1 at 0:38
1
@C.Swainston: Ideally,ActivityCompat.requestPermissions()
would do some sanity-checking on the inputs, so you'd fail with a better error message. I filed a feature request for that. Glad to hear that you got it working!
– CommonsWare
Jan 1 at 0:55
add a comment |
By looking at the source code, my best guess is that you are winding up with null
as one of the entries in the String
that you are passing to ActivityCompat.requestPermissions()
.
That might be due to something with how you are converting between arrays and lists. In particular, permissionsToRequest.toArray(permissions)
is kinda scary, as you are trying to reuse a String
that is passed in as a parameter. Plus, as Andy pointed out, toArray()
sets unused array elements to null
.
I'd allocate a fresh String
, as I do here:
private boolean hasPermission(String perm) {
return(ContextCompat.checkSelfPermission(this, perm)==
PackageManager.PERMISSION_GRANTED);
}
private String netPermissions(String wanted) {
ArrayList<String> result=new ArrayList<String>();
for (String perm : wanted) {
if (!hasPermission(perm)) {
result.add(perm);
}
}
return(result.toArray(new String[result.size()]));
}
By looking at the source code, my best guess is that you are winding up with null
as one of the entries in the String
that you are passing to ActivityCompat.requestPermissions()
.
That might be due to something with how you are converting between arrays and lists. In particular, permissionsToRequest.toArray(permissions)
is kinda scary, as you are trying to reuse a String
that is passed in as a parameter. Plus, as Andy pointed out, toArray()
sets unused array elements to null
.
I'd allocate a fresh String
, as I do here:
private boolean hasPermission(String perm) {
return(ContextCompat.checkSelfPermission(this, perm)==
PackageManager.PERMISSION_GRANTED);
}
private String netPermissions(String wanted) {
ArrayList<String> result=new ArrayList<String>();
for (String perm : wanted) {
if (!hasPermission(perm)) {
result.add(perm);
}
}
return(result.toArray(new String[result.size()]));
}
answered Jan 1 at 0:20
CommonsWareCommonsWare
772k13818841935
772k13818841935
Thank you! This fixed the problem. I would not have thought to look there. Also, thanks for the heads up on theINTERNET
permission.
– C. Swainston
Jan 1 at 0:38
1
@C.Swainston: Ideally,ActivityCompat.requestPermissions()
would do some sanity-checking on the inputs, so you'd fail with a better error message. I filed a feature request for that. Glad to hear that you got it working!
– CommonsWare
Jan 1 at 0:55
add a comment |
Thank you! This fixed the problem. I would not have thought to look there. Also, thanks for the heads up on theINTERNET
permission.
– C. Swainston
Jan 1 at 0:38
1
@C.Swainston: Ideally,ActivityCompat.requestPermissions()
would do some sanity-checking on the inputs, so you'd fail with a better error message. I filed a feature request for that. Glad to hear that you got it working!
– CommonsWare
Jan 1 at 0:55
Thank you! This fixed the problem. I would not have thought to look there. Also, thanks for the heads up on the
INTERNET
permission.– C. Swainston
Jan 1 at 0:38
Thank you! This fixed the problem. I would not have thought to look there. Also, thanks for the heads up on the
INTERNET
permission.– C. Swainston
Jan 1 at 0:38
1
1
@C.Swainston: Ideally,
ActivityCompat.requestPermissions()
would do some sanity-checking on the inputs, so you'd fail with a better error message. I filed a feature request for that. Glad to hear that you got it working!– CommonsWare
Jan 1 at 0:55
@C.Swainston: Ideally,
ActivityCompat.requestPermissions()
would do some sanity-checking on the inputs, so you'd fail with a better error message. I filed a feature request for that. Glad to hear that you got it working!– CommonsWare
Jan 1 at 0:55
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53992172%2frequesting-camera-permissions-results-in-nullpointerexception-in-string-equals%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
2
Your problem is inside of a
computePermissionGrantState()
, which is not shown in your question's source code.– CommonsWare
Dec 31 '18 at 23:49
1
@CommonsWare Forgive my ignorance, but I'm not even sure how to get to that code. By that I mean it's Android's code that I can't just click on from logcat.
– C. Swainston
Dec 31 '18 at 23:58
Sorry, I was misreading the stack trace, focusing too much on the class and not enough on the package. Let me putter through the source code and see if I can identify what might be wrong.
– CommonsWare
Jan 1 at 0:13
One thing that you can fix: get rid of
Manifest.permission.INTERNET
from yourcheckForPermissions()
call, as that is not adangerous
permission.– CommonsWare
Jan 1 at 0:15
1
The issue is with the
toArray
incheckForPermissions
- see List doc: If the list fits in the specified array with room to spare (i.e., the array has more elements than the list), the element in the array immediately following the end of the list is set to null. Most likely it is a case where you pruned the original list by at least 1.– Andy
Jan 1 at 0:19