gpt4 book ai didi

java - 从 fragment 替换 fragment 时 onRequestPermissionsResult 回调不起作用

转载 作者:行者123 更新时间:2023-12-02 12:22:48 28 4
gpt4 key购买 nike

我有一个具有 onRequestPermissionsResult 的 Activity ,每当我在运行时请求权限时就会调用该 Activity ,但当我从另一个 fragment 替换 fragment 时,它不会被调用。

流程是:我有 3 个 fragment fragment A、B 和 C。 fragment A 是附加到 MainActivity 的默认 fragment ,单击按钮时,我将 fragment A 替换为 fragment B,当我请求 fragment B 的权限时,它运行正常,但将 fragment B 替换为 fragment C 请求权限后会引发异常 fragment不依附于 Activity

主要 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);
}
}
}

fragment 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 代码 fragment

@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.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) 

最佳答案

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

关于java - 从 fragment 替换 fragment 时 onRequestPermissionsResult 回调不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45649076/

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