0

我需要在每天8:30在我的TaskManager中發出有關任務的通知。Android廣播通知NullPointerException意圖額外

下面是從活動我的代碼:

public final static String INTENT_LIST_FOR_NOTIFICATION = "tasks for notification"; 

private void setUserNotifications() { 
     long targetTime = targetCalendar.getTimeInMillis(); 
     long currentTime = targetCalendar.getTimeInMillis(); 

     Intent intent = new Intent(this, MyScheduledReceiver.class); 
     intent.putExtra(INTENT_LIST_FOR_NOTIFICATION, taskArrayList); 
     PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT); 
     if (currentTime >= targetTime) { 
      targetCalendar.add(Calendar.DAY_OF_MONTH, 1); 
      targetTime = targetCalendar.getTimeInMillis(); 
      alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, targetTime, AlarmManager.INTERVAL_DAY, pendingIntent); 
     } 
     else { 
      alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, targetTime, AlarmManager.INTERVAL_DAY, pendingIntent); 
     } 
    } 

在我的廣播接收機,我有這樣的代碼

public class MyScheduledReceiver extends BroadcastReceiver { 

    @Override 
    public void onReceive(Context context, Intent intent) { 
     Intent scheduledIntent = new Intent(context, MainActivity.class); 
     scheduledIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 

     PendingIntent contentIntent = PendingIntent.getActivity(context, 0, 
       scheduledIntent, 0); 

     NotificationManager notificationManager = (NotificationManager) context 
       .getSystemService(Context.NOTIFICATION_SERVICE); 
     ArrayList<MyTask> arrayList = (ArrayList<MyTask>) intent.getSerializableExtra(MainActivity.INTENT_LIST_FOR_NOTIFICATION); 
     for (int i = 0; i < arrayList.size(); i++) { 
      Notification notification = new Notification.Builder(context) 
        .setContentIntent(contentIntent) 
        .setContentTitle(arrayList.get(i).getmTaskName()) 
        .setSmallIcon(R.mipmap.ic_launcher) 
        .setTicker("") 
        .setWhen(System.currentTimeMillis()).setAutoCancel(true) 
        .build(); 

      notificationManager.notify(arrayList.get(i).getTaskId(), notification); 
     } 

    } 
} 

但我有問題與ArrayList的。

ArrayList<MyTask> arrayList = (ArrayList<MyTask>) intent.getSerializableExtra(MainActivity.INTENT_LIST_FOR_NOTIFICATION); 

後,它是空,並有空指針異常字符串

for (int i = 0; i < arrayList.size(); i++) { 

這裏是例外:

java.lang.RuntimeException: Unable to start receiver java.lang.NullPointerException: Attempt to invoke virtual method 'int java.util.ArrayList.size()' on a null object reference 
    at android.app.ActivityThread.handleReceiver(ActivityThread.java:3018) 
    at android.app.ActivityThread.-wrap18(ActivityThread.java) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1544) 
    at android.os.Handler.dispatchMessage(Handler.java:102) 
    at android.os.Looper.loop(Looper.java:154) 
    at android.app.ActivityThread.main(ActivityThread.java:6077) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756) 
    Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'int java.util.ArrayList.size()' on a null object reference 
    at com.example.user1.testtaskmanager.MyScheduledReceiver.onReceive(MyScheduledReceiver.java:25) 
    at android.app.ActivityThread.handleReceiver(ActivityThread.java:3011) 
    at android.app.ActivityThread.-wrap18(ActivityThread.java) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1544) 
    at android.os.Handler.dispatchMessage(Handler.java:102) 
    at android.os.Looper.loop(Looper.java:154) 
    at android.app.ActivityThread.main(ActivityThread.java:6077) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756) 

注意:它只能在24 API和未來。 Api 23和以前的工作很好。

回答

0

發送

intent.putStringArrayListExtra(MainActivity.INTENT_LIST_FOR_NOTIFICATION,taskArrayList) 

接收

intent.getStringArrayListExtra(MainActivity.INTENT_LIST_FOR_NOTIFICATION) 
+0

謝謝!我在代碼中做了一些改變,它可以工作。 –