在应用程序启动时收到此错误
日志:
-10594/com.example.messageswap E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.messageswap, PID: 10594
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.messageswap/com.example.messageswap.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.pm.PackageManager android.content.Context.getPackageManager()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3194)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7356)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
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:98)
at com.example.messageswap.MainActivity.<init>(MainActivity.java:63)
at java.lang.Class.newInstance(Native Method)
at android.app.AppComponentFactory.instantiateActivity(AppComponentFactory.java:95)
at androidx.core.app.CoreComponentFactory.instantiateActivity(CoreComponentFactory.java:45)
at android.app.Instrumentation.newActivity(Instrumentation.java:1243)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3182)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7356)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
我正在尝试允许我的android设备接收通过nfc发送数据的权限检查。
mainactivity.java:
PackageManager packageManager = getPackageManager();
public void permissionCheck(){
int permissionCheck = ContextCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_STATE);
if (permissionCheck != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_PHONE_STATE}, REQUEST_READ_PHONE_STATE);
} else {
//TODO
}
}
@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
switch (requestCode) {
case REQUEST_READ_PHONE_STATE:
if ((grantResults.length > 0) && (grantResults[0] == PackageManager.PERMISSION_GRANTED)) {
//TODO
}
break;
default:
break;
}
}
我知道自从安卓6.0以来,这是必须的。应用程序的目的是在android设备之间通过nfc发送和接收字符串。
提前谢谢。
2条答案
按热度按时间kwvwclae1#
根据堆栈跟踪,您正在尝试在
MainActivity
示例。这是不可能做到的。为了从活动中获取packagemanager,您的活动必须附加到应用程序类。这是在
attachBaseContext
生命周期方法。所以问题就在于:
为了解决这个问题,请将代码行修改为:
再加上
onCreate
方法:这将修复致命的异常。
请注意:该解决方案只是您问题的基本解决方案。我建议你不要储存
PackageManager
在你需要的任何时候PackageManager
-使用getPackageManager()
方法。rur96b6h2#
nfc未被列为危险许可https://developer.android.com/reference/android/manifest.permission#nfc 因此,它不需要任何运行时权限检查(如6.0及更高版本中“危险”权限所需)
因此
<uses-permission android:name="android.permission.NFC" />
够了吗