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
我在另一个Activity中使用完全相同的代码行并且它正在工作,但不是在这里。怎么样?
答案 0 :(得分:1)
你不能在构造函数中获得getContext,你可以进入onCreate()。查看代码,您是否在类的构造函数中使用newNotification()?