Android 意图额外广播通知 NullPointerException

Android broadcast notification NullPointerException on intent Extra

我需要每天在 8:30.

中发送有关我的 TaskManager 中的任务的通知

这是我来自 activity 的代码:

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

它是 null 并且在字符串中有 NullPointer Exception

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)

注意:仅在API 24 及以后。 Api 23 和以前的工作正常。

发送中

intent.putStringArrayListExtra(MainActivity.INTENT_LIST_FOR_NOTIFICATION,taskArrayList)

正在接收

intent.getStringArrayListExtra(MainActivity.INTENT_LIST_FOR_NOTIFICATION)