java 使用 android.content.Context.checkPermission 检查权限时出现空指针异常
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35844879/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Null pointer exception when checking for permission with android.content.Context.checkPermission
提问by Async-
I need to check for permissions before querying the Android calendar for events. To do it, Android studio is warning that I need to follow a check before querying. The auto generated code is this piece:
在查询 Android 日历的事件之前,我需要检查权限。为此,Android Studio 警告我需要在查询之前进行检查。自动生成的代码是这样的:
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_CALENDAR) != PackageManager.PERMISSION_GRANTED) {
System.out.println("NO ACCESS TO CALENDAR!! Abort mission, abort mission!!");
}
When trying to run it, I get this error:
尝试运行它时,我收到此错误:
Attempt to invoke virtual method 'int
android.content.Context.checkPermission(java.lang.String, int, int)' on a null object reference
尝试
在空对象引用上调用虚拟方法“int android.content.Context.checkPermission(java.lang.String, int, int)”
So it is clear that something is null at this point, and I tried to get the context of the app with a different way, but it's still the same error. Other thing I tried was this code, which is supposed to handle the targets lower than Android 6:
所以很明显,此时有些东西是空的,我试图用不同的方式获取应用程序的上下文,但它仍然是同样的错误。我尝试的另一件事是这段代码,它应该处理低于 Android 6 的目标:
if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_CALENDAR) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_CALENDAR},
MY_PERMISSIONS_REQUEST_READ_CONTACTS);
}
Still get the same error, can anybody help me with this?
仍然出现同样的错误,有人可以帮我解决这个问题吗?
回答by CommonsWare
it's a separate class, controller: public class DummyData extends Activity { .... }
它是一个单独的类,控制器:public class DummyData extends Activity { .... }
That is not going to work.
那是行不通的。
Neverextend Activity
unless it is a real activity, one that you will register in the manifest.
永远不要扩展,Activity
除非它是一项真正的活动,您将在清单中注册的活动。
Nevercreate an instance of an Activity
via a constructor (e.g., the new DummyData()
that you have somewhere in your code). Use startActivity()
to display an activity that you have registered in the manifest.
永远不要Activity
通过构造函数创建 an 的实例(例如,new DummyData()
您在代码中的某处)。使用startActivity()
显示一个活动,您在清单已注册。
As it stands, while your DummyData
class may work from a compilation standpoint, it will not work at runtime. An Activity
needs to be instantiated by the framework, and that is not the case with your DummyData
.
就目前而言,虽然您的DummyData
类可以从编译的角度工作,但它在运行时不起作用。AnActivity
需要由框架实例化,而您的DummyData
.
Pass a realContext
object to checkSelfPermission()
, and pass a realActivity
object to requestPermissions()
. In this case, "real" means "handed to you from the framework".
将真实Context
对象checkSelfPermission()
传递给,并将真实Activity
对象传递给requestPermissions()
。在这种情况下,“真实”的意思是“从框架交给你”。
回答by Ramesh R
Use (Activity)mContext instead of this.
使用 (Activity)mContext 而不是这个。
if(ContextCompat.checkSelfPermission((Activity)mContext,Manifest.permission.READ_CALENDAR)!=PackageManager.PERMISSION_GRANTED)
{
ActivityCompat.requestPermissions((Activity) mContext, new String[]{Manifest.permission.READ_CALENDAR},
MY_PERMISSIONS_REQUEST_READ_CONTACTS);
}
回答by Muhammad Kashif Arif
You have to write the right activity at the position "this" main problem in activity.
您必须在活动中的“这个”主要问题的位置编写正确的活动。
Try to write the code in MainActivity and test.
尝试在 MainActivity 中编写代码并进行测试。
if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_CALENDAR) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_CALENDAR},
MY_PERMISSIONS_REQUEST_READ_CONTACTS);
}