gpt4 book ai didi

android - 请求 READ_CONTACTS 权限 Android M 时出错

转载 作者:行者123 更新时间:2023-11-30 01:20:30 42 4
gpt4 key购买 nike

我正在尝试获取 Android M 的 READ_CONTACTS 权限并正在调用:

ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_CONTACTS}, 1);

应用崩溃,出现以下异常:

05-12 17:08:24.578: E/AndroidRuntime(2846): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.android.packageinstaller/com.android.packageinstaller.permission.ui.GrantPermissionsActivity}: java.lang.NullPointerException: Attempt to get length of null array
05-12 17:08:24.578: E/AndroidRuntime(2846): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
05-12 17:08:24.578: E/AndroidRuntime(2846): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
05-12 17:08:24.578: E/AndroidRuntime(2846): at android.app.ActivityThread.-wrap11(ActivityThread.java)
05-12 17:08:24.578: E/AndroidRuntime(2846): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
05-12 17:08:24.578: E/AndroidRuntime(2846): at android.os.Handler.dispatchMessage(Handler.java:102)
05-12 17:08:24.578: E/AndroidRuntime(2846): at android.os.Looper.loop(Looper.java:148)
05-12 17:08:24.578: E/AndroidRuntime(2846): at android.app.ActivityThread.main(ActivityThread.java:5417)
05-12 17:08:24.578: E/AndroidRuntime(2846): at java.lang.reflect.Method.invoke(Native Method)
05-12 17:08:24.578: E/AndroidRuntime(2846): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
05-12 17:08:24.578: E/AndroidRuntime(2846): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
05-12 17:08:24.578: E/AndroidRuntime(2846): Caused by: java.lang.NullPointerException: Attempt to get length of null array
05-12 17:08:24.578: E/AndroidRuntime(2846): at com.android.packageinstaller.permission.ui.GrantPermissionsActivity.computePermissionGrantState(GrantPermissionsActivity.java:293)
05-12 17:08:24.578: E/AndroidRuntime(2846): at com.android.packageinstaller.permission.ui.GrantPermissionsActivity.updateDefaultResults(GrantPermissionsActivity.java:343)
05-12 17:08:24.578: E/AndroidRuntime(2846): at com.android.packageinstaller.permission.ui.GrantPermissionsActivity.onCreate(GrantPermissionsActivity.java:100)
05-12 17:08:24.578: E/AndroidRuntime(2846): at android.app.Activity.performCreate(Activity.java:6251)
05-12 17:08:24.578: E/AndroidRuntime(2846): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
05-12 17:08:24.578: E/AndroidRuntime(2846): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)

我还指定了 <uses-permission android:name="android.permission.READ_CONTACTS" />在 list 文件中。

最佳答案

查看 GrantPermissionsActivity 的来源, 在 computePermissionGrantState() 中唯一的地方你可以获得NullPointerException的方法来自 Attempt to get length of null array在这一行中:

for (int i = 0; i < callingPackageInfo.requestedPermissions.length; i++) {

既然你已经声明你确实列出了 READ_CONTACTS list 中的许可,它会遵循它只是在错误的地方,而且实际上不在那里。确保 <uses-permission>元素在 <application> 之外标签解决了这个问题。

关于android - 请求 READ_CONTACTS 权限 Android M 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37185927/

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