发送未决意图警报管理器时应用程序崩溃

App crashing when sending pending intent Alarm Manager

理想情况下,我想在应用程序暂停时调用,但为了让它正常工作,我从按钮调用该方法。我在一个 ArrayList 中有一个 ArrayList,它有一个时间列表,如果列表和项目设置为活动,我想发送给警报管理器。如果有人能帮助我理解为什么会失败,我将不胜感激。

这是我的代码:

public class ReminderList extends AppCompatActivity {

    public static ArrayList<ReminderType> AllReminders=new ArrayList<>();
    public reminderCAdapter adapter;
    public static ReminderType currentReminderType;
    public static ItemType currentItemType;
    public static TimeType currentTimesType;
    Context context;
    AlarmManager alarmMan;
    PendingIntent pIntent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.context=this;
        setContentView(R.layout.activity_reminder_list);
        //generate list
            try {
                LoadData();
            } catch (IOException | ClassNotFoundException | JSONException e) {
                e.printStackTrace();
            }
        alarmMan=(AlarmManager) getSystemService(ALARM_SERVICE);

        adapter=new reminderCAdapter(AllReminders,this);

        //handle listview and assign adapter
        ListView lView = (ListView)findViewById(R.id.listView);
        lView.setAdapter(adapter);
    }

    //Iterates through all reminders to see if reminder is enabled and if item is enabled and has a time
    public void CheckAllActiveReminders(View view){
        //Go through each reminder
        Log.e("1", "1");
        for(int i=0;i<AllReminders.size();i++){
            //if the reminder is active continue
            Log.e("2", "1");
            if(AllReminders.get(i).ReminderActive){
                //Go through each item
                Log.e("3", "1");
                for (int j=0;j<AllReminders.get(i).Items.size();j++){
                    //if the item is active
                    Log.e("4", "1");
                    if(AllReminders.get(i).Items.get(j).ItemActive){
                        //Go through each timetype
                        Log.e("5", "1");
                        for (int h=0;h<AllReminders.get(i).Items.get(j).Times.size();h++){
                            Log.e("6", "1");
                            Intent rIntent=new Intent(this.context, Alarm_Receiver.class);
                            Log.e("7", "1");
                            pIntent=PendingIntent.getBroadcast(ReminderList.this,0, rIntent,PendingIntent.FLAG_UPDATE_CURRENT);
                            Log.e("8", AllReminders.get(i).ReminderName+", "+AllReminders.get(i).Items.get(j).ItemName+", "+AllReminders.get(i).Items.get(j).Times.get(h).displayTime);
                            alarmMan.set(AlarmManager.RTC_WAKEUP,AllReminders.get(i).Items.get(j).Times.get(h).cal.getTimeInMillis(),pIntent);
                        }
                    }else{
                        //If item is deactivated all times in it need to be cancelled
                        Log.e("Fail 2", "1");
                    }
                }
            }else{
                //if reminder is not active all items in it need to be deactivated
                Log.e("Fail 1", "1");
            }
        }

    }
}

我已经能够确认它一直到 alarmMan.set 因为日志显示它。这是堆栈跟踪:

10-01 19:26:01.059 11560-11560/com.example.poods.remindmeandlogit E/8: Reminder, Item, 7:25 PM 10-01 19:26:01.059 11560-11560/com.example.poods.remindmeandlogit E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.poods.remindmeandlogit, PID: 11560 java.lang.IllegalStateException: Could not execute method for android:onClick at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:293) at android.view.View.performClick(View.java:4438) at android.view.View$PerformClick.run(View.java:18422) at android.os.Handler.handleCallback(Handler.java:733) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:136) at android.app.ActivityThread.main(ActivityThread.java:5017) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:515) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) at dalvik.system.NativeStart.main(Native Method) Caused by: java.lang.reflect.InvocationTargetException at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:515) at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288) at android.view.View.performClick(View.java:4438)  at android.view.View$PerformClick.run(View.java:18422)  at android.os.Handler.handleCallback(Handler.java:733)  at android.os.Handler.dispatchMessage(Handler.java:95)  at android.os.Looper.loop(Looper.java:136)  at android.app.ActivityThread.main(ActivityThread.java:5017)  at java.lang.reflect.Method.invokeNative(Native Method)  at java.lang.reflect.Method.invoke(Method.java:515)  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)  at dalvik.system.NativeStart.main(Native Method)  Caused by: java.lang.NullPointerException at com.example.poods.remindmeandlogit.ReminderList.CheckAllActiveReminders(ReminderList.java:84) at java.lang.reflect.Method.invokeNative(Native Method)  at java.lang.reflect.Method.invoke(Method.java:515)  at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)  at android.view.View.performClick(View.java:4438)  at android.view.View$PerformClick.run(View.java:18422)  at android.os.Handler.handleCallback(Handler.java:733)  at android.os.Handler.dispatchMessage(Handler.java:95)  at android.os.Looper.loop(Looper.java:136)  at android.app.ActivityThread.main(ActivityThread.java:5017)  at java.lang.reflect.Method.invokeNative(Native Method)  at java.lang.reflect.Method.invoke(Method.java:515)  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)  at dalvik.system.NativeStart.main(Native Method) 

Caused by: java.lang.NullPointerException at com.example.poods.remindmeandlogit.ReminderList.CheckAllActiveReminders(ReminderList.java:84)

第 84 行是什么?您正在那里访问空对象。 参见 What is a NullPointerException, and how do I fix it?