0

I am creating a scheduler application. In that I am setting multiple events using AlarmManager and PendingIntent's. I use following methods:

Here I declare PendingIntent array:

public static ArrayList <PendingIntent> intentArray= new ArrayList <PendingIntent>();

Here I am adding intents to the array:

PendingIntent pendingIntent = PendingIntent.getBroadcast(
        EditScheduleActivity.this, intentid, notifyintent,
        PendingIntent.FLAG_UPDATE_CURRENT); //intent id is unique

alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
        pendingIntent);
MainActivity.intentArray.add(pendingIntent);

Here I am clearing an alarm:

EditScheduleActivity.alarmManager.cancel(intentArray.get(selecteditemid_int));
intentArray.remove(selecteditemid_int);

But when I clear an alarm, I am getting an ArrayIndexOutOfBoundsException,I think the problem is when I restart application, intent array list gets re-initialized and throws exception. How can I overcome this problem by keeping the intent array list not to re-initialized?

Basim Sherif
  • 5,384
  • 7
  • 48
  • 90

2 Answers2

0

I think the problem is when i restart application intent array list gets re-initialized and throws exception.

In your case you have to use Sharepreference or database, store arraylist into shareprence and while intentArray.remove(selecteditemid_int); update your SP, so next time it will give you last stored values, check below links.

Using preference API in Android applications

Stackoverflow Answer

Community
  • 1
  • 1
RobinHood
  • 10,897
  • 4
  • 48
  • 97
-1

This has very little to do with Android specifically. An ArrayOutOfBoundsException means that when you do:

intentArray.remove(selecteditemid_int);

The selecteditemid_int is greater than the length of the array.

LuxuryMode
  • 33,401
  • 34
  • 117
  • 188
  • Yes thats what I have told. Intent array gets re-initialized when I restart app.And the array list got cleared.thats why I got array out of bound exception.I need to keep the intent array not to re-initialized.How can I do that? – Basim Sherif Mar 01 '13 at 05:15