gpt4 book ai didi

java - 请求相机权限导致 String.equals 中的 NullPointerException

转载 作者:搜寻专家 更新时间:2023-11-01 09:22:11 25 4
gpt4 key购买 nike

我正在尝试请求相机权限以在我的 Android 应用程序中使用 Google OCR,但在大多数设备上它在安装时崩溃。

我读过类似的帖子说这是由传递到 ContextCompat.checkSelfPermission() 的空上下文引起的,但我看不出上下文如何为空。

这是我的代码:

    @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 实现了 SurfaceHolder.Callback,这就是相机实际初始化的地方。

这是异常(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)

最佳答案

通过查看源代码,我的最佳猜测是您最终将 null 作为您要传递给的 String[] 中的条目之一ActivityCompat.requestPermissions()

这可能与您在数组和列表之间的转换方式有关。特别是,permissionsToRequest.toArray(permissions) 有点可怕,因为您正试图重用作为参数传入的 String[]。另外,as Andy pointed out , toArray() 将未使用的数组元素设置为 null

我会像我一样分配一个新的 String[] 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()]));
}

关于java - 请求相机权限导致 String.equals 中的 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53992172/

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