Null pointer in getApplicationContext ()

I am trying to execute the following code snippet where the service is executing 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 () works fine, but the following exception is thrown inside the overridden method:

 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 assume that I am missing an important Java concept. Can't use getApplicationContext () inside an overridden method?

+7
source share
6 answers

You should try to avoid using getApplicationContext() as much as possible, as this will greatly increase the likelihood of getting Force Closes.

Use YourClass.this , in this case MyListenerClass.this .

I think this does not work in this example because getApplicationContext() is called after stopSelf()

+7
source

Not sure if you can, since this seems to be inside the service, but if it works onCreate (and this path is actually called), you can easily save the context in a local variable in onCreate , and then use it later in your method.

0
source

Toasts can only be added to an Activity as a context.

0
source

You declared it as a new process, in this case you can get a null pointer exception

 android:process=":MyListenerClass" 

In the context of the process, getApplicationContext () will not work.

You can verify this by checking logcat to see if the TIDs and PIDs are active and services for you.

0
source

Try giving MyListenerClass.this or YourActivity.this

eg.

 Toast.makeText(MyListenerClass.this, "Service Stopped", Toast.LENGTH_LONG).show(); 
0
source

You must create a context for the activity and pass that context.

  public static void pushList(Activity context){ static Context = mConext; protected void onPostExecute(String result) { Toast toast=Toast.makeText(mConext,"Succefully Updated Profile",Toast.LENGTH_LONG); toast.show(); } } 

it really works for me.

-one
source

All Articles