我有一个无头片段在Marshmallow设备的运行时询问权限。
public class ReadContactPermissionHelper extends Fragment {
private static final int REQUEST_READ_CONTACT_PERMISSION = 9001;
public static final String TAG = "ReadContactPermission";
private Activity activity;
private static boolean isReadContactPermissionDenied;
private OnReadContactPermissionCallback mCallback;
public static ReadContactPermissionHelper newInstance() {
return new ReadContactPermissionHelper();
}
public ReadContactPermissionHelper() {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRetainInstance(true);
}
@TargetApi(23)
@Override
public void onAttach(Context context) {
super.onAttach(context);
onAttachToContext(context);
}
@SuppressWarnings("deprecation")
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
if (Build.VERSION.SDK_INT < 23) {
onAttachToContext(activity);
}
}
protected void onAttachToContext(Context context) {
if (context instanceof Activity) {
activity = (Activity) context;
if (activity instanceof OnReadContactPermissionCallback) {
mCallback = (OnReadContactPermissionCallback) activity;
} else {
throw new IllegalArgumentException("Activity must implement ReadContactPermissionCallback");
}
}
}
public void checkReadContactPermission() {
if (PermissionUtil.hasSelfPermission(activity, new String[]{Manifest.permission.READ_CONTACTS})) {
mCallback.onReadContactPermissionGranted();
} else {
if (!isReadContactPermissionDenied) {
requestPermissions(new String[]{Manifest.permission.READ_CONTACTS}, REQUEST_READ_CONTACT_PERMISSION);
}
}
}
public static void setReadContactPermissionDenied(boolean cameraMicPermissionDenied) {
isReadContactPermissionDenied = cameraMicPermissionDenied;
}
public static boolean isReadContactPermissionDenied() {
return isReadContactPermissionDenied;
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions,
int[] grantResults) {
if (requestCode == REQUEST_READ_CONTACT_PERMISSION) {
if (PermissionUtil.verifyPermissions(grantResults)) {
ReadContactPermissionHelper.setReadContactPermissionDenied(false);
mCallback.onReadContactPermissionGranted();
} else {
ReadContactPermissionHelper.setReadContactPermissionDenied(true);
mCallback.onReadContactPermissionDenied();
}
} else {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
}
public interface OnReadContactPermissionCallback {
void onReadContactPermissionGranted();
void onReadContactPermissionDenied();
}
}
在我需要此权限的活动中,我在无头片段中调用checkReadContactPermission
,然后执行权限检查任务。
在活动中,我有以下内容 -
ReadContactPermissionHelper mReadContactPermissionHelper = (ReadContactPermissionHelper) getSupportFragmentManager().findFragmentByTag(ReadContactPermissionHelper.TAG);
if (mReadContactPermissionHelper == null) {
mReadContactPermissionHelper = ReadContactPermissionHelper.newInstance();
getSupportFragmentManager().beginTransaction()
.add(mReadContactPermissionHelper, ReadContactPermissionHelper.TAG)
.commit();
}
mReadContactPermissionHelper.checkReadContactPermission();
但是,在检查权限时,调用checkReadContactPermission()
会导致应用程序崩溃,因为Activity
为空 -
日志
FATAL EXCEPTION: main
Process: com.sachingutte.cardmeup, PID: 12087
java.lang.RuntimeException: Unable to start activity com.sachingutte.example{com.sachingutte.example/com.sachingutte.example.AddPhoneContactActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'int android.app.Activity.checkSelfPermission(java.lang.String)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'int android.app.Activity.checkSelfPermission(java.lang.String)' on a null object reference
at com.sachingutte.example.helpers.PermissionUtil.hasSelfPermission(PermissionUtil.java:60)
at com.sachingutte.example.helpers.ReadContactPermissionHelper.checkReadContactPermission(ReadContactPermissionHelper.java:69)
at com.sachingutte.example.AddPhoneContactActivity.onCreate(AddPhoneContactActivity.java:151)
at android.app.Activity.performCreate(Activity.java:6251)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
问题在于调用PermissionUtil.hasSelfPermission(activity, new String[]{Manifest.permission.READ_CONTACTS}))
时,正在传递null
个活动。 onAttach
方法未被调用。