无法启动活动ComponentInfo,请求权限

时间:2018-05-11 14:58:55

标签: java android permissions

我尝试在Android上申请权限,但我遇到了错误。

Unable to start activity ComponentInfo {[...]}:java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.pm.PackageManager android.content.Context.getPackageManager()' on a null object reference    
[...]  
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.pm.PackageManager android.content.Context.getPackageManager()' on a null object reference  
at android.content.ContextWrapper.getPackageManager(ContextWrapper.java:91)  
at android.app.Activity.requestPermissions(Activity.java:4284)  
at android.support.v4.app.ActivityCompat.requestPermissions(ActivityCompat.java:507)  
at com.appwifi.Wifi.Wifi.setPermission(Wifi.java:118)  
at com.appwifi.Wifi.Wifi.connect(Wifi.java:62)  
at com.appwifi.Wifi.Wifi.<init>(Wifi.java:51)  
at com.appwifi.Main.MainActivity.onCreate(MainActivity.java:32)  

我想我已经理解它是一个NullPointerException,但即使使用调试器,我也无法找到null。 这是代码:

public class Wifi extends Activity {
    public static final int WIFI_PERMISSION = 1;
    [...]
    private void setPermission() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M
            && (ActivityCompat.checkSelfPermission(mContext, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED
            || ActivityCompat.checkSelfPermission(mContext, Manifest.permission.CHANGE_WIFI_STATE) != PackageManager.PERMISSION_GRANTED
            || ActivityCompat.checkSelfPermission(mContext, Manifest.permission.ACCESS_WIFI_STATE) != PackageManager.PERMISSION_GRANTED)) {
        ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_WIFI_STATE,Manifest.permission.CHANGE_WIFI_STATE,Manifest.permission.ACCESS_COARSE_LOCATION},WIFI_PERMISSION); // Error is on this line
    }
}

&#39;这&#39;不能为空,新的字符串和静态数字

我知道有一些类似的主题,但每个都非常具体,我没有找到任何解决方案。 不明白请帮助我。

0 个答案:

没有答案