Android API 26 SecurityException issue

2019-08-28 08:31发布

问题:

In my app I have a fragment and an imageButton inside of this fragment. When I run the app in my device (Android 8 - API 26) and click imageButton, the app crashes and throws a runtime error which I didn't see before.

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)

So I added the required permissions in Manifest.xml and also tried every codes in the topic Android permission.INTERACT_ACROSS_USERS_FULL but I get the same error. I have solved Autofill problem with this topic but in that fragment error occurs just only I clicked ImageButton. What should I do to solve this problem?

回答1:

Finally I found out the solution. Problem occurs since I had a method in my activity named getUserId() that returns String user_id and send data to fragments. I changed it's name and now everything works fine.



回答2:

You have to add runtime permission. To do that follow

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

Module checkIfAlreadyhavePermission() is implemented as :

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

Module requestForSpecificPermission() is implemented as :

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

and Override in 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);
    }
}