Null Pointer in getApplicationContext()
You should try to avoid using getApplicationContext()
as much as possible, as this will highly increase the chance of getting Force Closes.
Instead use YourClass.this
, in this case MyListenerClass.this
.
I guess in this example it doesn't work, because getApplicationContext()
is called after stopSelf()
Saurabh Verma
Lead Data Scientist (Applied Machine Learning space) with a background of Data Engineering and a solid foundation in Data Structures and Algorithms. My toybox includes Python, scikit-learn, Keras, pytorch, mysql, redis, Java, etc. Have been recently working on various nlp applications. Co-Founder: MetricsWiki (http://metricswiki.com), a platform for preparing for engineering competitive exams
Updated on June 17, 2022Comments
-
Saurabh Verma almost 2 years
I'm trying the following piece of code, where a service is implementing my listener:
public class MyListenerClass extends Service implements MyListenerInterface { public void onCurrencyRecieved(MyEventClass event) { System.out.println("Coins Recieved - Listener Successful"); stopSelf(); Toast toast = Toast.makeText(getApplicationContext(), "Service Stopped", Toast.LENGTH_LONG); toast.show(); } @Override public void onCreate() { Toast toast = Toast.makeText(getApplicationContext(),"Service started", Toast.LENGTH_LONG); toast.show(); super.onCreate(); }
Now, the toast inside onCreate() is working fine, but that inside the overridden method is throwing the following exception:
01-03 18:52:35.740: ERROR/AndroidRuntime(2388): java.lang.NullPointerException 01-03 18:52:35.740: ERROR/AndroidRuntime(2388): at android.content.ContextWrapper.getApplicationContext(ContextWrapper.java:100) 01-03 18:52:35.740: ERROR/AndroidRuntime(2388): at com.test.listenertest1.MyListenerClass.onCurrencyRecieved(MyListenerClass.java:28) 01-03 18:52:35.740: ERROR/AndroidRuntime(2388): at com.test.listenertest1.MyEventGenerator.generateEvent(MyEventGenerator.java:34) 01-03 18:52:35.740: ERROR/AndroidRuntime(2388): at com.test.listenertest1.MyEventGenerator.<init>(MyEventGenerator.java:16) 01-03 18:52:35.740: ERROR/AndroidRuntime(2388): at com.test.listenertest1.NewActivity.onKeyDown(NewActivity.java:33) 01-03 18:52:35.740: ERROR/AndroidRuntime(2388): at android.view.KeyEvent.dispatch(KeyEvent.java:1037) 01-03 18:52:35.740: ERROR/AndroidRuntime(2388): at android.app.Activity.dispatchKeyEvent(Activity.java:2046) 01-03 18:52:35.740: ERROR/AndroidRuntime(2388): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:1631) 01-03 18:52:35.740: ERROR/AndroidRuntime(2388): at android.view.ViewRoot.deliverKeyEventToViewHierarchy(ViewRoot.java:2368) 01-03 18:52:35.740: ERROR/AndroidRuntime(2388): at android.view.ViewRoot.handleFinishedEvent(ViewRoot.java:2338) 01-03 18:52:35.740: ERROR/AndroidRuntime(2388): at android.view.ViewRoot.handleMessage(ViewRoot.java:1641) 01-03 18:52:35.740: ERROR/AndroidRuntime(2388): at android.os.Handler.dispatchMessage(Handler.java:99)
I guess I'm missing some important java concept. Cant we use getApplicationContext() inside an overridden method ?
-
Steve Blackwell over 12 yearsWhy does calling
getApplicationContext()
increase the chance of getting a Force Close? -
Force over 12 yearsFor example if you call it in an overriden OnClick Method it might - depending on the device/firmware/sdk - crash. Or see the code above: if
this
was used, it would not throw the Exception. -
Saurabh Verma over 12 yearsI have tried using MyListenerClass.this and even removed stopself(). Still it is crashing giving the same log. Moreover I didnt see anything in the logs which suggests that it's a firmware crash
-
Saurabh Verma over 12 yearsInside onCreate() I tried this: mContext = MyListenerClass.this; and then used mCOntext inside my overridden method. Still it's crashing
-
Force over 12 yearsDid you override
onCurrencyRecieved
? Because in the code above you didn't. -
Saurabh Verma over 12 yearsonCurrencyRecieved is my listener method
-
Force over 12 yearsHave you tried replacing the toast with a Log.d(),? And if yes, did it still crash?