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

这是我的通知活动,但是当我调用它时,我收到一条错误消息:

prefs = new AppPreferences(getApplicationContext());

LogCat 说:

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

我在另一个活动中使用完全相同的代码行并且它正在工作,但不是在这里。怎么来的?

4

1 回答 1

1

你不能在构造函数中获取上下文,你可以在 onCreate() 中获取。查看您的代码,您是否在类的构造函数中使用 newNotification() ?

于 2012-12-19T23:12:00.887 回答