Pointeur Null dans getcontexteapplication()

J'essaie le morceau de code suivant, où un service est la mise en œuvre de mon auditeur:

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();
    }

Maintenant, le toast à l'intérieur onCreate() fonctionne très bien, mais qu'à l'intérieur de la méthode de remplacement est de lancer l'exception suivante:

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)

Je suppose que je suis en manque quelques java importants concept. Cant nous utilisons getcontexteapplication() à l'intérieur d'une méthode surchargée ?

Selon le manuel de l' (developer.android.com/reference/android/app/Service.html), vous devez être en mesure de le faire. Ce qui est à l'intérieur de stopSelf()?
Rien n'est à l'intérieur de stopself(). Le Service continue de fonctionner jusqu'à ce que stopself() est appelée

OriginalL'auteur Saurabh Verma | 2012-01-03