我試圖通過AlarmManager延遲通知。爲了這個目的,我創建了目前看起來像這樣的AlarmReciever:我創建了一個TestActivty,我嘗試啓動這樣一個報警後通知延遲
public class NotificationAlarmReciever extends BroadcastReceiver{
public static final String TAG = "NotificationAlarmReciever";
public static final String KEY_NOTIFICATION_EXTRA = "NotificationExtra";
public static final String KEY_NOTIFICATION_ID = "NotificationId";
@Override
public void onReceive(Context context, Intent intent) {
LogCat.d(TAG, "AlarmReciever Triggered");
LogIntent(intent);
Bundle b = intent.getExtras();
if (b != null){
if (b.containsKey(KEY_NOTIFICATION_EXTRA)){
Notification n = b.<Notification>getParcelable(KEY_NOTIFICATION_EXTRA);
int request = 100
if(b.containsKey(KEY_NOTIFICATION_ID)){
request = b.getInt(KEY_NOTIFICATION_ID)
}
NotificationManager manager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
manager.notify(request, n);
}
}
}
private void LogIntent(Intent intent) {
//logs Intent....
}
}
:
public void onClick(View v) {
Context c = getApplicationContext();
int requestCode = 333;
Notification notification = new Notification(R.drawable.balloon_overlay_close,"ticker", System.currentTimeMillis() + Integer.parseInt(mWhen.getText().toString()));
Intent notificationAction = new Intent(c, TestActivity.class);
PendingIntent notificationPendingOperation = PendingIntent.getActivity(c, requestCode, notificationAction, 0);
notification.setLatestEventInfo(c, "HEADER", "MESSAGE", notificationPendingOperation);
AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
Intent alarmIntent = new Intent(c, NotificationAlarmReciever.class);
alarmIntent.putExtra(NotificationAlarmReciever.KEY_NOTIFICATION_EXTRA, notification);
PendingIntent alarmOperation = PendingIntent.getBroadcast(c, requestCode, alarmIntent, PendingIntent.FLAG_UPDATE_CURRENT);
//Line 127
alarmManager.set(requestCode, notification.when, alarmOperation);
}
只要alarmManager.set叫我得到一個NullPointerException異常說:
08-01 16:37:15.592: ERROR/AndroidRuntime(4422): FATAL EXCEPTION: main
08-01 16:37:15.592: ERROR/AndroidRuntime(4422): java.lang.NullPointerException
08-01 16:37:15.592: ERROR/AndroidRuntime(4422): at android.os.Parcel.readException(Parcel.java:1328)
08-01 16:37:15.592: ERROR/AndroidRuntime(4422): at android.os.Parcel.readException(Parcel.java:1276)
08-01 16:37:15.592: ERROR/AndroidRuntime(4422): at android.app.IAlarmManager$Stub$Proxy.set(IAlarmManager.java:174)
08-01 16:37:15.592: ERROR/AndroidRuntime(4422): at android.app.AlarmManager.set(AlarmManager.java:139)
08-01 16:37:15.592: ERROR/AndroidRuntime(4422): at de.eos.uptrade.android.core.TestActivity$1.onClick(TestActivity.java:127)
08-01 16:37:15.592: ERROR/AndroidRuntime(4422): at android.view.View.performClick(View.java:2485)
08-01 16:37:15.592: ERROR/AndroidRuntime(4422): at android.view.View$PerformClick.run(View.java:9080)
08-01 16:37:15.592: ERROR/AndroidRuntime(4422): at android.os.Handler.handleCallback(Handler.java:587)
08-01 16:37:15.592: ERROR/AndroidRuntime(4422): at android.os.Handler.dispatchMessage(Handler.java:92)
08-01 16:37:15.592: ERROR/AndroidRuntime(4422): at android.os.Looper.loop(Looper.java:130)
08-01 16:37:15.592: ERROR/AndroidRuntime(4422): at android.app.ActivityThread.main(ActivityThread.java:3683)
08-01 16:37:15.592: ERROR/AndroidRuntime(4422): at java.lang.reflect.Method.invokeNative(Native Method)
08-01 16:37:15.592: ERROR/AndroidRuntime(4422): at java.lang.reflect.Method.invoke(Method.java:507)
08-01 16:37:15.592: ERROR/AndroidRuntime(4422): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
08-01 16:37:15.592: ERROR/AndroidRuntime(4422): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
08-01 16:37:15.592: ERROR/AndroidRuntime(4422): at dalvik.system.NativeStart.main(Native Method)
我不知道,我做錯了什麼。
你在onclick方法中得到了一個nullpointerexception。請確定發生異常的確切行。然後嘗試推斷該行中訪問的對象爲空。然後更新你的問題。 – Marmoy
@videre我的OnClick方法是你看到的第二個 - 我在哪裏創建報警 - 127行是alarmManager.set –
好吧,你知道如何調試嗎?在第127行放置一個斷點,運行應用程序,並在程序到達斷點時,將光標懸停在第127行中的每個對象上,直到找到一個爲空的對象。然後在這裏更新 – Marmoy