简体   繁体   English

服务调用 checkSelfPermission 返回 nullPointerException

[英]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.

相关问题 为什么checkSelfPermission()抛出NullPointerException - Why checkSelfPermission() is throwing a NullPointerException 服务未初始化,返回NullPointerException - Service is not initialized, returns NullPointerException NullPointerException with checkSelfPermission-我在找对地方吗? - NullPointerException with checkSelfPermission - Am I looking in the right place? Android M Permissions:checkSelfPermission()始终返回-1 - Android M Permissions : checkSelfPermission() always returns -1 代码从 If(ActivityCompat.checkSelfPermission) 段返回 - Code Returns from If(ActivityCompat.checkSelfPermission) segment HttpTransportSE.call方法频繁返回NullPointerException - HttpTransportSE.call method returns NullPointerException frequently Android的ContextCompat.checkSelfPermission()返回不正确的值 - Android's ContextCompat.checkSelfPermission() returns incorrect value android ContextCompat.checkSelfPermission始终返回PERMISSION_GRANTED - android ContextCompat.checkSelfPermission always returns PERMISSION_GRANTED 即使第一次安装应用程序,checkSelfPermission 总是返回拒绝 - checkSelfPermission always returns denied even if app is installed for first time 尽管已授予WRITE_EXTERNAL_STORAGE,但ContextCompat.checkSelfPermission返回-1 - ContextCompat.checkSelfPermission returns -1 despite WRITE_EXTERNAL_STORAGE granted
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM