[英]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
类(如Activity
或Context
)分配给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.this或getApplicationContext()
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.