Android ContextWrapper.Java:100

public void newNotification(CharSequence message) {

    prefs = new AppPreferences(getApplicationContext());

    Intent i = new Intent(GameNotification.this, ChallengeList.class);

    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, i, 0);

    NotificationManager nm = (NotificationManager)
                getSystemService(NOTIFICATION_SERVICE);

    Notification notif = new Notification(R.drawable.icon, "Ny utmaning!", System.currentTimeMillis());


    notif.setLatestEventInfo(this, "Triviation", message + " utmanar dig!", pendingIntent);

    notif.defaults |= Notification.DEFAULT_ALL;
    notif.flags |= Notification.FLAG_AUTO_CANCEL;

    //notif.vibrate = new long [] {0, 1000};

    nm.notify(prefs.id(), notif);
}

This is my Notification-activity but when I call it I get an error on the line:

prefs = new AppPreferences(getApplicationContext());

LogCat says this:

05-03 18:20:13.441: E/AndroidRuntime(1688): java.lang.RuntimeException: Unable to start         receiver se.saxman.triviation.C2DMReceiver: java.lang.NullPointerException
05-03 18:20:13.441: E/AndroidRuntime(1688):     at android.app.ActivityThread.handleReceiver(ActivityThread.java:1809)
05-03 18:20:13.441: E/AndroidRuntime(1688):     at android.app.ActivityThread.access$2400(ActivityThread.java:117)
05-03 18:20:13.441: E/AndroidRuntime(1688):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:985)
05-03 18:20:13.441: E/AndroidRuntime(1688):     at android.os.Handler.dispatchMessage(Handler.java:99)
05-03 18:20:13.441: E/AndroidRuntime(1688):     at android.os.Looper.loop(Looper.java:130)
05-03 18:20:13.441: E/AndroidRuntime(1688):     at android.app.ActivityThread.main(ActivityThread.java:3687)
05-03 18:20:13.441: E/AndroidRuntime(1688):     at java.lang.reflect.Method.invokeNative(Native Method)
05-03 18:20:13.441: E/AndroidRuntime(1688):     at java.lang.reflect.Method.invoke(Method.java:507)
05-03 18:20:13.441: E/AndroidRuntime(1688):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842)
05-03 18:20:13.441: E/AndroidRuntime(1688):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
05-03 18:20:13.441: E/AndroidRuntime(1688):     at dalvik.system.NativeStart.main(Native Method)
05-03 18:20:13.441: E/AndroidRuntime(1688): Caused by: java.lang.NullPointerException
05-03 18:20:13.441: E/AndroidRuntime(1688):     at android.content.ContextWrapper.getApplicationContext(ContextWrapper.java:100)
05-03 18:20:13.441: E/AndroidRuntime(1688):     at se.saxman.triviation.GameNotification.newNotification(GameNotification.java:15)
05-03 18:20:13.441: E/AndroidRuntime(1688):     at se.saxman.triviation.C2DMReceiver.onReceive(C2DMReceiver.java:63)
05-03 18:20:13.441: E/AndroidRuntime(1688):     at android.app.ActivityThread.handleReceiver(ActivityThread.java:1798)
05-03 18:20:13.441: E/AndroidRuntime(1688):     ... 10 more

I use the exact same line of code in another Activity and it's working, but not here. How come?

preguntado el 03 de mayo de 12 a las 17:05

how this activity was called/created? ... i'm pretty sure that you create it using new GameNotification(...) ... and activity isn't initiated ... -

Yes, GM = new GameNotification(); Should I do otherwise? -

An activity has to be started through startActivity (from a different activity). The method getApplicationContext only works when it is created that way, and after onCreate has been executed. -

But I don't want to start the activity, just call the void and get a notification. -

anyway ... you're using C2DM ... to show notification you dont need activity .... just start service(IntentService) (AFAIK there is example in Chrome to Phone app) and show notification from service (Service is context too) ... you can start Activity if user interact with notification ... -

1 Respuestas

You can't getContext in constructor you can get in onCreate(). Look in your code, do you use newNotification() in constructors of class?

Respondido el 19 de diciembre de 12 a las 23:12

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.