在android [duplicate]中运行时检查权限时获取NullPointerException
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了在android [duplicate]中运行时检查权限时获取NullPointerException相关的知识,希望对你有一定的参考价值。
这个问题在这里已有答案:
这是MainActivity代码。从MainActivity的onCreate()方法调用MultiplePermission类的permissionCheck()方法。
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if(Build.VERSION.SDK_INT >= 23){
MultiplePermission multiplePermission = new MultiplePermission();
if(multiplePermission.permissionCheck()){
Toast.makeText(this, "All permission granted", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(this, "Permission denied", Toast.LENGTH_SHORT).show();
}
}
}
}
MultiplePermission类代码如下。此类的permissionCheck()方法在运行时检查android权限。如果拒绝任何权限,则关闭整个应用程序。
public class MultiplePermission extends AppCompatActivity {
private int PERMISSION_ALL = 1;
private String[] PERMISSIONS = {
Manifest.permission.RECORD_AUDIO,
Manifest.permission.READ_PHONE_STATE,
Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.PROCESS_OUTGOING_CALLS
};
public boolean permissionCheck() {
if (!hasPermissions(this, PERMISSIONS)) {
ActivityCompat.requestPermissions(this, PERMISSIONS, PERMISSION_ALL);
}
return true;
}
private boolean hasPermissions(Context context,String[] permissions) {
for (String permission : permissions) {
if (ActivityCompat.checkSelfPermission((Activity)context, permission) != PackageManager.PERMISSION_GRANTED) {
return false;
}
}
return true;
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
for(int result : grantResults){
if(result != PackageManager.PERMISSION_GRANTED)
System.exit(0);
}
return;
}
}
这是错误信息。
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'int android.content.Context.checkPermission(java.lang.String, int, int)' on a null object reference
at android.content.ContextWrapper.checkPermission(ContextWrapper.java:669)
at android.support.v4.content.ContextCompat.checkSelfPermission(ContextCompat.java:453)
at testapp.android.vogella.com.permissiontest.MultiplePermission.hasPermissions(MultiplePermission.java:40)
at testapp.android.vogella.com.permissiontest.MultiplePermission.permissionCheck(MultiplePermission.java:29)
at testapp.android.vogella.com.permissiontest.MainActivity.onCreate(MainActivity.java:16)
at android.app.Activity.performCreate(Activity.java:6664)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
答案
你永远不应该用Activity
开始一个new Object()
,它不会得到context
参考。
MultiplePermission
不应该是Activity
,并且在你需要上下文的方法中,通过参数得到它:
public boolean permissionCheck(Activity activity) {
if (!hasPermissions(this, PERMISSIONS)) {
ActivityCompat.requestPermissions(activity, PERMISSIONS, PERMISSION_ALL);
}
return true;
}
以上是关于在android [duplicate]中运行时检查权限时获取NullPointerException的主要内容,如果未能解决你的问题,请参考以下文章
应用程序正在模拟器中定位,但在实际的 android 设备中运行时没有