最是人间留不住,朱颜辞镜花辞树。这篇文章主要讲述在android [duplicate]中运行时检查权限时获取NullPointerException相关的知识,希望能为你提供帮助。
这个问题在这里已有答案:
- What is a NullPointerException, and how do I fix it? 12个答案
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)
答案【在android [duplicate]中运行时检查权限时获取NullPointerException】你永远不应该用
Activity
开始一个new Object()
,它不会得到context
参考。MultiplePermission
不应该是Activity
,并且在你需要上下文的方法中,通过参数得到它:public boolean permissionCheck(Activity activity) {
if (!hasPermissions(this, PERMISSIONS)) {ActivityCompat.requestPermissions(activity, PERMISSIONS, PERMISSION_ALL);
}
return true;
}
推荐阅读
- 尝试调用虚方法'void android.view.View.setOnClickListener [duplicate]
- safari app extensions(从swift后台进程向所有选项卡广播消息)
- Android findViewById空指针[重复]
- 从Safari打开相机APP
- Java applet无法在Safari 7下打开文件(Mac OS X 10.9)
- Safari Webapp强制在单击其他链接时打开Safari
- 限制Google API的Android密钥使用
- 如何在没有app包限制的情况下使用map v2 api key()
- 使用Android应用无法在Google云端硬盘上看到PC上传的文件