[英]Service call checkSelfPermission returns nullPointerException
I have a service checking if an android device has a permission to write to external storage by using this code:我有一项服务,通过使用以下代码检查 android 设备是否有权写入外部存储:
checkSelfPermission(android.Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED checkSelfPermission(android.Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED
which always return NullPointerException总是返回 NullPointerException
public class Repeater extends Service {
public Repeater() throws Exception {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (checkSelfPermission(android.Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED)
{
Toast.makeText(getApplicationContext(),"sasa",Toast.LENGTH_SHORT).show();
} else {
Log.v(TAG,"Permission is revoked");
}
}
else { //permission is automatically granted on sdk<23 upon installation
Log.v(TAG,"Permission is granted");
}
}
}
Full stack trace完整的堆栈跟踪
java.lang.NullPointerException: Attempt to invoke virtual method
'java.lang.Object android.content.Context.getSystemService(java.lang.String)' on a null object reference
at android.content.ContextWrapper.getSystemService(ContextWrapper.java:659)
at com.presentiva.repeater.Repeater.isStoragePermissionGranted(Repeater.java:49)
at com.presentiva.repeater.Repeater.<init>(Repeater.java:30)
at java.lang.Class.newInstance(Native Method)
at android.app.ActivityThread.handleCreateService(ActivityThread.java:3173)
at android.app.ActivityThread.-wrap5(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1567)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
Problem: You check permissions in constructor of the service, but at that time, the context for the service has not been initialized yet, so that why the app throws the exception.问题:您在服务的构造函数中检查权限,但当时服务的上下文尚未初始化,因此应用程序抛出异常。
Solution: Put your code to check permissions in onCreate() method of the service.解决方案:将您的代码放在服务的onCreate()方法中检查权限。
public class Repeater extends Service {
@Override
public void onCreate() {
super.onCreate();
// Context for this service has been initialized.
try {
checkPermissions();
} catch (Exception e) {
e.printStackTrace();
}
}
private void checkPermissions() throws Exception {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (checkSelfPermission(android.Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
Toast.makeText(getApplicationContext(), "sasa", Toast.LENGTH_SHORT).show();
} else {
Log.v(TAG, "Permission is revoked");
}
} else { //permission is automatically granted on sdk<23 upon installation
Log.v(TAG, "Permission is granted");
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.