0

Im getting error at getApplicationContext() when I am setting an notification in my app through my adapter class which extends PagerAdapter. Below is my code:

Calendar cal = Calendar.getInstance();
cal.set(Calendar.MONTH,6);
cal.set(Calendar.YEAR, 2013);               
cal.set(Calendar.DAY_OF_MONTH, 26);
cal.set(Calendar.HOUR_OF_DAY,12);
cal.set(Calendar.MINUTE,31);
//cal.add(Calendar.MINUTE, 5);
Intent alarmintent = new Intent(getApplicationContext(), AlarmReceiver.class);
alarmintent.putExtra("title","Reminder");
alarmintent.putExtra("note"," is in few minutes.");
PendingIntent sender = PendingIntent.getBroadcast(getApplicationContext(), HELLO_ID,
        alarmintent,PendingIntent.FLAG_UPDATE_CURRENT|  Intent.FILL_IN_DATA);
//  AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), sender);

can anyone plz help me. Best Regards.

Boris Mocialov
  • 3,439
  • 2
  • 28
  • 55
  • 1
    what error are you getting? copy/paste from logcat. If it is `NullPointerException` that you are getting, try using your `activity context` – Boris Mocialov Jul 26 '13 at 07:39
  • Can you try putting the current context instead of Application context. – tasomaniac Jul 26 '13 at 07:40
  • Im getting error "create method getApplicationContext()" –  Jul 26 '13 at 07:44
  • maybe this will help : http://stackoverflow.com/a/13523542/1276374 – Boris Mocialov Jul 26 '13 at 07:48
  • When I use the same code with Activity context, 1)Intent alarmintent = new Intent(context, AlarmReceiver.class);2)AlarmManager am = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE); at this time no errors but Im unable to set alarm –  Jul 26 '13 at 07:51

1 Answers1

1

you can save context of your calling activiy in a private variable of your adapter class. and pass that to constructor of intent instead of getApplicationContext().

private myAdapter extends PagerAdapter{
private Context mContext;
.
.
.
public myAdapter(Context context,........... ) {

super(context,  ......);
this.mContext = context;
.
.
}



//now in constructor of Intent
Intent alarmintent = new Intent(mContext, AlarmReceiver.class);
//also in pending intent

Pankaj
  • 46
  • 2
  • I used Activity context; will this do –  Jul 26 '13 at 07:54
  • please elaborate more i assume you want to say you passed your constructor myAdapter(Activity context,......) than you can simply make your private variable to be private Activity mContext – Pankaj Jul 26 '13 at 08:35