简体   繁体   中英

App crashing when sending pending intent Alarm Manager

Ideally I'd like to call whenever the app is paused but for the sake of getting it to work I'm calling the method from a button. I've got an ArrayList within an ArrayList that has a list of times I'd like to have sent to the alarm manager if the list and item are set to active. If anyone can help me make sense of why this is failing I'd greatly appreciate it.

Here's my code:

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

    }
}

I've been able to confirm that it makes it all the way until alarmMan.set because the log shows it. Here's the stack trace:

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)

What is line 84?, you're accessing a null object there. See What is a NullPointerException, and how do I fix it?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM