android - PendingIntent null when trying to call it from a class -
im bulding alarm clock, , got pendingintent should snooze alarm. same code worked great when in activity. change things, , in different class. nullpointerexecption. (the contractor gets context activity.
snooze function:
public void settingnewintentforsnooze() { pendingintent alarmpendingintent=null; calendar calendar = calendar.getinstance(); // new alarm after snooze. calendar.set(calendar.minute, calendar.get(calendar.minute) + snoozetime); intent snoozeintent = new intent(context, alarm_reciver.class); snoozeintent.putextra("click_status", true); //add try solve problem... snoozeintent.setaction("snooze"); alarmpendingintent = pendingintent.getbroadcast(context, 0, snoozeintent, alarmpendingintent.flag_one_shot); //tells phone set alarm alarm_manager.set(alarmmanager.rtc_wakeup, calendar.gettimeinmillis(), alarmpendingintent); }
alarmpage activity(the code creats variable) :
snooze= new snooze(this,alarm_manager);
alarmreciver:
public void onreceive(context context, intent intent) { log.e("in alarm reciver","in alarm reciver"); intent service_intent= new intent(context, ringtoneplayingservice.class);//intent service ringtone playing boolean button_status = intent.getextras().getboolean("click_status"); service_intent.putextra("click_status", button_status);//passing button status context.startservice(service_intent);//start ringtone service }
the error:
09-17 18:25:25.564 1563-1563/com.example.itay.newfrindlyalarm e/androidruntime: fatal exception: main process: com.example.itay.newfrindlyalarm, pid: 1563 java.lang.nullpointerexception: attempt invoke virtual method 'void android.app.alarmmanager.set(int, long, android.app.pendingintent)' on null object reference @ com.example.itay.newfrindlyalarm.snooze.settingnewintentforsnooze(snooze.java:60) @ com.example.itay.newfrindlyalarm.snooze.operate(snooze.java:44) @ com.example.itay.newfrindlyalarm.alarmpage.operatesnooze(alarmpage.java:88) @ com.example.itay.newfrindlyalarm.alarmpage$3.onclick(alarmpage.java:68) @ android.view.view.performclick(view.java:6256) @ android.view.view$performclick.run(view.java:24697) @ android.os.handler.handlecallback(handler.java:789) @ android.os.handler.dispatchmessage(handler.java:98) @ android.os.looper.loop(looper.java:164) @ android.app.activitythread.main(activitythread.java:6541) @ java.lang.reflect.method.invoke(native method) @ com.android.internal.os.zygote$methodandargscaller.run(zygote.java:240) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:767) enter code here
judging error message, possible alarm_manager
null
? if initialize alarmmanager
properly, error go away?
Comments
Post a Comment