“非静态变量不能......”错误 - MarshMallow权限

时间:2016-05-11 12:00:14

标签: android android-6.0-marshmallow android-permissions

我正在尝试为Marshmallow 6.0实现permissionChecker

在我的onCreate中,我放了权限检查方法(这是每次onCreate()上发生错误的地方,请参阅下面的错误)。

@Override
public void onCreate() {

    Permission(); 
    //other codes here
}

这是在MainActivity类中,在onCreate()

上调用
    //Permision code that will be checked in the method onRequestPermissionsResult
    public int STORAGE_PERMISSION_CODE = 23;

    //We are calling this method to check the permission status
    public boolean isReadStorageAllowed() {
        //Getting the permission status
        int result = ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE);

        //If permission is granted returning true
        if (result == PackageManager.PERMISSION_GRANTED)
            return true;

        //If permission is not granted returning false
        return false;
    }

    //Requesting permission
    public void requestStoragePermission(){

        if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.READ_EXTERNAL_STORAGE)){
            //If the user has denied the permission previously your code will come to this block
            //Here you can explain why you need this permission
            //Explain here why you need this permission
        }

        //And finally ask for the permission
        ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, STORAGE_PERMISSION_CODE);
    }

    //This method will be called when the user will tap on allow or deny
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {

        //Checking the request code of our request
        if (requestCode == STORAGE_PERMISSION_CODE) {

            //If permission is granted
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

                //Displaying a toast
                Toast.makeText(this, "Permission granted now you can read the storage", Toast.LENGTH_LONG).show();
            } else {
                //Displaying another toast if permission is not granted
                Toast.makeText(this, "Oops you just denied the permission", Toast.LENGTH_LONG).show();
            }
        }
    }

    public void Permission(){
        ////Android marshmallow new Permission Code////
        if (isReadStorageAllowed()) {
            //If permission is already having then showing the toast
            return;
        }
        //If permission is already having then showing the toast
        else{
            requestStoragePermission();

        }
    }

}

只有在权限关闭的情况下才会发生这种情况,如果我从设置中手动启用它就可以了。

  

“主要活动出错”

     

引起:java.lang.NullPointerException:尝试获取null数组的长度                                                        在com.nephi.motionmusicplayer_2mp.MusicService.updateSongList(MusicService.java:210)                                                        在com.nephi.motionmusicplayer_2mp.MusicService.init(MusicService.java:323)                                                        在com.nephi.motionmusicplayer_2mp.MusicService.onCreate(MusicService.java:110)

0 个答案:

没有答案