从片段替换片段时,onRequestPermissionsResult 回调不起作用
Posted
技术标签:
【中文标题】从片段替换片段时,onRequestPermissionsResult 回调不起作用【英文标题】:onRequestPermissionsResult callback not working when replacing fragment from a fragment 【发布时间】:2018-01-20 18:58:15 【问题描述】:我有一个具有 onRequestPermissionsResult 的活动,每当我在运行时请求权限时都会调用它,但是当我从另一个片段替换片段时它不会被调用。
过程是: 我有 3 个片段 片段 A、B 和 C。 片段 A 是附加到 MainActivity 的默认片段,单击按钮时,我将片段 A 替换为片段 B,当我请求片段 B 的权限时,它运行正常,但将片段 B 替换为片段 C 请求权限后抛出异常 fragment未附加到活动
MainActivity 代码:
@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 ResultInfowho=@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.access$1600(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.access$1600(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)
【问题讨论】:
【参考方案1】:您是否有任何具体原因向您的活动请求权限?您可以只处理每个片段中的权限。因为片段有自己的requestPermissions()
和onRequestPermissionsResult
方法。例如见this
【讨论】:
非常感谢你让我开心,再次感谢我学会了在片段中请求运行时权限的新方法 很高兴为您提供帮助。干杯以上是关于从片段替换片段时,onRequestPermissionsResult 回调不起作用的主要内容,如果未能解决你的问题,请参考以下文章