简体   繁体   English

如何更好地理解错误以进行修复

[英]How to get better understanding of error in order to fix

I'm receiving the following error message in LogCat: 我在LogCat中收到以下错误消息:

   java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Context.getPackageName()' on a null object reference

I'm aware of what a NullPointerException is but not 100% on how to fix this with regards to passing the correct context. 我知道什么是NullPointerException,但不是100%关于如何通过正确的上下文解决此问题。 The error only happens when the app is runnning in the background (multitasking) A little guidance would be much appreciated. 仅当应用程序在后台运行(多任务)时才会发生该错误。 please Logcat and offending code below. 请在下面输入Logcat和有问题的代码。 THanks 谢谢

Logcat: logcat的:

Process: com.app.app, PID: 17519
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Context.getPackageName()' on a null object reference
    at android.preference.PreferenceManager.getDefaultSharedPreferencesName(PreferenceManager.java:537)
    at android.preference.PreferenceManager.getDefaultSharedPreferences(PreferenceManager.java:526)
    at com.app.app.DatabaseHandling.UpdateData.<init>(UpdateData.java:70)
    at com.app.app.PushService.PushReceiver$1.run(PushReceiver.java:94)
    at java.lang.Thread.run(Thread.java:764)

PushReceiver PushReceiver

UpdateData updateData = new UpdateData(MainActivity.mainActivity);

UpdateData: 的UpdateData:

 private final SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(MainActivity.mainActivity);

most likely (based upon the limited example provided, which does not even indicate, within which context that code runs), it should rather be: 最有可能(基于提供的有限示例,甚至没有指出代码在哪个上下文中运行),它应该是:

SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(MainActivity.this);

because one cannot just make up static fields, which do not exist - 因为一个人不能仅仅组成不存在的静态字段-

while assigning Context classes (alike Activity or Context ) to static fields is generally bad practice... and should be avoided, whenever just possible. 虽然将Context类(如ActivityContext )分配给static字段通常是一种不好的做法……并且应尽可能避免使用。

I assume the problem is, that you try to initialise the SharedPreferences sp at the location, where you define it. 我认为问题是,您尝试在定义它的位置初始化SharedPreferences sp You should define it first like: 您应该首先定义它,例如:

private SharedPreferences sp;

After that, set this global variable sp in a function like "onReceive(Context context)": 之后,在“ onReceive(Context context)”之类的函数中设置此全局变量sp

sp = PreferenceManager.getDefaultSharedPreferences(context);

Or like already mentioned in the Activity itself in the function "onCreate(...)": 或者像已经在Activity本身中在函数“ onCreate(...)”中提到的那样:

sp = PreferenceManager.getDefaultSharedPreferences(MainActivity.this);

The problem might be, that you try to initialize a variable with a context (like activity), that is not available at this point, but in a later step of the lifecycle. 问题可能在于,您尝试使用上下文(如活动)初始化变量,该变量目前尚不可用,但在生命周期的后续步骤中不可用。

And avoid to hand over a context supplied by a static variable from another class. 并避免移交给另一个类的静态变量提供的上下文。

First you shuld check your code. 首先,您应该检查您的代码。 you are passing MainActivity.mainAcitvity instead of MainACtivity.this or getApplicationContext() 您正在传递MainActivity.mainAcitvity而不是MainACtivity.thisgetApplicationContext()

private final SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(MainActivity.mainActivity);

always pass context of activity in which your method works ... 总是传递您的方法在其中起作用的活动的上下文...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Android中修复尺寸调整图像中获得更好的质量? - How to get better quality in fix resize image in Android? 哪种方法更好才能获得方向? - Which method is better in order to get the direction? 如何修复 kotlin 中的错误 GET 请求 API? - How to fix an error GET request API in kotlin? 如何使用正确的 Crashlytics 错误分组获得更好的 RxJava 堆栈跟踪 - How to get better RxJava stacktraces with correct Crashlytics error grouping 在哪里可以更好地了解使用Context参数实例化的整体和深入知识? - Where can I get a better understanding of the overall and indepth understanding of instantiation with Context parameters? 更好地了解onTouchListener - better understanding of onTouchListener 如何更好地模块化ViewPager订单/标签名称 - How to better modularize the ViewPager order/tab names 为什么我会收到 Kotlin.Unit 错误以及如何解决? - Why do i get the Kotlin.Unit error and how to fix it? 如何修复在 Android 12 上获取应用程序版本错误? - How to fix error get app version on Android 12? 更好地了解SimpleAdapter的ViewBinder - Better understanding of the SimpleAdapter's ViewBinder
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM