Android API 26 SecurityException 问题

Android API 26 SecurityException issue

在我的应用程序中,我有一个片段和一个位于该片段内的图像按钮。当我 运行 设备中的应用程序 (Android 8 - API 26) 并单击 imageButton 时,应用程序崩溃并抛出一个我没有看到的 运行time 错误之前。

E/AndroidRuntime: FATAL EXCEPTION: main
              Process: com.example.mustafa.diyetisyenadmin, PID: 26366
              java.lang.SecurityException: Permission Denial: null asks to run as user 1 but is calling from user 0; this requires android.permission.INTERACT_ACROSS_USERS_FULL or android.permission.INTERACT_ACROSS_USERS
                  at android.os.Parcel.readException(Parcel.java:1945)
                  at android.os.Parcel.readException(Parcel.java:1891)
                  at android.view.autofill.IAutoFillManager$Stub$Proxy.addClient(IAutoFillManager.java:326)
                  at android.view.autofill.AutofillManager.ensureServiceClientAddedIfNeededLocked(AutofillManager.java:903)
                  at android.view.autofill.AutofillManager.notifyViewExited(AutofillManager.java:487)
                  at android.view.View.notifyEnterOrExitForAutoFillIfNeeded(View.java:6983)
                  at android.view.View.dispatchAttachedToWindow(View.java:17594)
                  at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3332)
                  at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3339)
                  at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3339)
                  at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3339)
                  at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3339)
                  at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3339)
                  at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3339)
                  at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1792)
                  at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1515)
                  at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:7266)
                  at android.view.Choreographer$CallbackRecord.run(Choreographer.java:981)
                  at android.view.Choreographer.doCallbacks(Choreographer.java:790)
                  at android.view.Choreographer.doFrame(Choreographer.java:721)
                  at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:967)
                  at android.os.Handler.handleCallback(Handler.java:808)
                  at android.os.Handler.dispatchMessage(Handler.java:101)
                  at android.os.Looper.loop(Looper.java:166)
                  at android.app.ActivityThread.main(ActivityThread.java:7425)
                  at java.lang.reflect.Method.invoke(Native Method)
                  at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:245)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:921)

所以我在 Manifest.xml 中添加了所需的权限,并尝试了主题 中的每个代码,但我得到了同样的错误。我已经解决了这个主题的自动填充问题,但在那个片段错误中,只有我点击了 ImageButton。我应该怎么做才能解决这个问题?

您必须添加运行时权限。为此,请遵循

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                if (!checkIfAlreadyhavePermission()) {
                    requestForSpecificPermission();
                }
}

模块 checkIfAlreadyhavePermission() 实现为:

private boolean checkIfAlreadyhavePermission() {
    int result = ContextCompat.checkSelfPermission(this, Manifest.permission.INTERACT_ACROSS_USERS_FULL);
    return result == PackageManager.PERMISSION_GRANTED;
}

模块 requestForSpecificPermission() 实现为:

private void requestForSpecificPermission() {
    ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.INTERACT_ACROSS_USERS_FULL}, 101);
}

并在 Activity 中覆盖:

@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
    switch (requestCode) {
        case 101:
            if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                //DO WHATEVER YOU WANTED TO DO
            } else {
                //not granted
            }
            break;
        default:
            super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }
}

终于找到解决办法了。出现问题是因为我的 activity 中有一个名为 getUserId() 的方法 returns String user_id 并将数据发送到片段。我更改了它的名称,现在一切正常。