soulqw/SoulPermission

Vivo手机授权失败Crash

Yofun opened this issue · 3 comments

Yofun commented

机型:vivo X7
版本:Android 7.1

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final TextView textView = findViewById(R.id.main_text_view);
        SoulPermission.getInstance().checkAndRequestPermissions(
                Permissions.build(
                        Manifest.permission.READ_PHONE_STATE,
                        Manifest.permission.ACCESS_COARSE_LOCATION,
                        Manifest.permission.ACCESS_FINE_LOCATION
                ),
                //if you want do noting or no need all the callbacks you may use SimplePermissionsAdapter instead
                new CheckRequestPermissionsListener() {
                    @Override
                    public void onAllPermissionOk(Permission[] allPermissions) {
                        Location location = Util.getLocation(MainActivity.this);
                        String str = "Latitude>>>>" + location.getLatitude() + "\nLongitude" + location.getLongitude();
                        textView.setText(str);
                        Log.d(TAG, str);
                    }

                    @Override
                    public void onPermissionDenied(Permission[] refusedPermissions) {
                        Toast.makeText(MainActivity.this, "授权失败", Toast.LENGTH_SHORT).show();
                    }
                });
    }

异常信息:

2019-06-04 21:15:43.779 18381-18381/com.hyfun.soulpermission E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.hyfun.soulpermission, PID: 18381
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.hyfun.soulpermission/com.hyfun.soulpermission.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'double android.location.Location.getLatitude()' on a null object reference
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2784)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2845)
        at android.app.ActivityThread.-wrap12(ActivityThread.java)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1562)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:154)
        at android.app.ActivityThread.main(ActivityThread.java:6306)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1108)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:998)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'double android.location.Location.getLatitude()' on a null object reference
        at com.hyfun.soulpermission.MainActivity$1.onAllPermissionOk(MainActivity.java:36)
        at com.qw.soul.permission.SoulPermission.checkAndRequestPermissions(SoulPermission.java:169)
        at com.hyfun.soulpermission.MainActivity.onCreate(MainActivity.java:25)
        at android.app.Activity.performCreate(Activity.java:6804)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1125)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2737)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2845) 
        at android.app.ActivityThread.-wrap12(ActivityThread.java) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1562) 
        at android.os.Handler.dispatchMessage(Handler.java:102) 
        at android.os.Looper.loop(Looper.java:154) 
        at android.app.ActivityThread.main(ActivityThread.java:6306) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1108) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:998) 

```


描述:进入MainActivity后,在onCreate方法中调用,拒绝后会回调onAllPermissionOk(Permission[] allPermissions)方法,所以才会造成空指针异常

你好,首先看崩溃日志应该不是库里面的崩溃,你细看一下 Location 类的获取是不是没做判空处理。
至于为什么会走到onAllPermissionOk,你确认下是否有在Manifest文件中声明相关权限,如果声明了权限, 还是走到这个回调的话,就应该是厂商自己实现的权限系统。
如何验证:
1.你在 onAllPermissionOk 里面方法体注释掉只加个日志,再重启一次,如果即没出授权弹框,又走到了这个回调。
2.你在onCreate 中单独调用 Location location = Util.getLocation(MainActivity.this); 是否会出授权弹框
确认以上两点的话,应该就是正常的操作了,厂商自定义权限系统也是默认授权成功的,细节上你可以看下博客

Yofun commented

vivo手机是自己定义了权限系统,就是这个原因,所以在点击拒绝授权的时候也会走onAllPermissionOk的回调,所以才会报location为null的错。这种情况该怎么处理呢?拒绝授权后还会走onAllPermissionOk的这种情况

问题原因:https://blog.csdn.net/aabbwoshishei/article/details/76022152

比如你这里,还是同样的代码,在调用位置的代码处try/catch ,异常提示用户手动开启权限