从片段中替换片段时,onRequestPermissionsResult 回调不起作用

onRequestPermissionsResult callback not working when replacing fragment from a fragment

我有一个 activity,它有 onRequestPermissionsResult,每当我在运行时请求权限时都会调用它,但是当我从另一个片段替换片段时,它不会被调用。

过程是: 我有 3 个片段 片段 A、B 和 C。 片段 A 是附加到 MainActivity 的默认片段,单击按钮时我将片段 A 替换为片段 B,当我请求片段 B 的权限时,它是 运行 正确但在将片段 B 替换为片段 C 后请求权限抛出异常片段未附加到 activity

主要活动代码:

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    List<Fragment> fragments = getSupportFragmentManager().getFragments();
    if (fragments != null) {
        for (Fragment fragment : fragments) {

                fragment.onRequestPermissionsResult(requestCode, permissions, grantResults);
        }
    }
}

片段B代码

Fragment fragment = new ShedDetailFragment();
        Bundle args=new Bundle();
        args.putString("flockno",flockno);
        args.putString("shedno",shedno.get(position));
        fragment.setArguments(args);
        // Insert the fragment by replacing any existing fragment
        FragmentManager fragmentManager = getFragmentManager();
        fragmentManager.beginTransaction()
                .add(fragment,"ShedDetailsFrament")
                .replace(R.id.frame_container, fragment)
                .commit();

片段C代码

@Override
public void onRequestPermissionsResult(int requestCode,
                                       String permissions[], int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);

    switch (requestCode) {
        case 12:{
            // If request is cancelled, the result arrays are empty.
            if (grantResults.length > 0
                    && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

                // permission was granted, yay! Do the
                // contacts-related task you need to do.
                takePhotoFromCamera();
                //Toast.makeText(getActivity(),"Permission Granted for Camera.Please Select Farm Photo",Toast.LENGTH_SHORT).show();

            } else {

                // permission denied, boo! Disable the
                // functionality that depends on this permission.
            }
            return;
        }
        case 11:{
            // If request is cancelled, the result arrays are empty.
            if (grantResults.length > 0
                    && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

                // permission was granted, yay! Do the
                // contacts-related task you need to do.
                saveImage(bitmap);
                //Toast.makeText(getActivity(),"Permission Granted for Storage.Please Select Farm Photo",Toast.LENGTH_SHORT).show();

            } else {

                // permission denied, boo! Disable the
                // functionality that depends on this permission.
            }
            return;
        }
        // other 'case' lines to check for other
        // permissions this app might request
    }
}

允许任何权限时的错误日志

E/AndroidRuntime: FATAL EXCEPTION: main
              Process: com.artevet.artevet, PID: 32741
              java.lang.RuntimeException: Failure delivering result ResultInfo{who=@android:requestPermissions:, request=12, result=-1, data=Intent { act=android.content.pm.action.REQUEST_PERMISSIONS (has extras) }} to activity {com.artevet.artevet/com.artevet.artevet.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v4.app.Fragment.onRequestPermissionsResult(int, java.lang.String[], int[])' on a null object reference
                  at android.app.ActivityThread.deliverResults(ActivityThread.java:4998)
                  at android.app.ActivityThread.handleSendResult(ActivityThread.java:5041)
                  at android.app.ActivityThread.access00(ActivityThread.java:229)
                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1875)
                  at android.os.Handler.dispatchMessage(Handler.java:102)
                  at android.os.Looper.loop(Looper.java:148)
                  at android.app.ActivityThread.main(ActivityThread.java:7325)
                  at java.lang.reflect.Method.invoke(Native Method)
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
               Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v4.app.Fragment.onRequestPermissionsResult(int, java.lang.String[], int[])' on a null object reference
                  at com.artevet.artevet.MainActivity.onRequestPermissionsResult(MainActivity.java:383)
                  at android.app.Activity.dispatchRequestPermissionsResult(Activity.java:7291)
                  at android.app.Activity.dispatchActivityResult(Activity.java:7169)
                  at android.app.ActivityThread.deliverResults(ActivityThread.java:4994)
                  at android.app.ActivityThread.handleSendResult(ActivityThread.java:5041) 
                  at android.app.ActivityThread.access00(ActivityThread.java:229) 
                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1875) 
                  at android.os.Handler.dispatchMessage(Handler.java:102) 
                  at android.os.Looper.loop(Looper.java:148) 
                  at android.app.ActivityThread.main(ActivityThread.java:7325) 
                  at java.lang.reflect.Method.invoke(Native Method) 
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230) 
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120) 

您向 activity 请求权限是否有任何具体原因?您可以只处理每个片段中的权限。因为片段有自己的 requestPermissions()onRequestPermissionsResult 方法。例如,参见