public class AlarmTask implements Runnable{
// The date selected for the alarm
private final Calendar date;
// The android system alarm manager
private final AlarmManager am;
// Your context to retrieve the alarm manager from
private final Context context;
public AlarmTask(Context context, Calendar date) {
this.context = context;
this.am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); //error for this line
this.date = date;
}
也有類似的問題,但敲我的頭我後無法解決問題。我收到以下錯誤!是由於碎片?錯誤:試圖調用虛擬方法java.lang.Object繼承android.content.Context.getSystemService(java.lang.String中)「對空對象引用
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object android.content.Context.getSystemService(java.lang.String)' on a null object reference
at android.content.ContextWrapper.getSystemService(ContextWrapper.java:562)
at lol.com.epl.AlarmTask.<init>(AlarmTask.java:24)
at lol.com.epl.ScheduleService.setAlarm(ScheduleService.java:47)
at lol.com.epl.ScheduleClient.setAlarmForNotification(ScheduleClient.java:58)
at lol.com.epl.FixFragment.onCreateView(FixFragment.java:88)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:1962)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1026)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1207)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:738)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1572)
at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:493)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5257)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
這是怎麼了調用類
public void setAlarm(Calendar c) {
// This starts a new thread to set the alarm
// You want to push off your tasks onto a new thread to free up the UI to carry on responding
new AlarmTask(this, c).run();
}
這就是我所說的setAlarm功能
public void setAlarmForNotification(Calendar c){
ScheduleService mBoundService = new ScheduleService();
mBoundService.setAlarm(c);
}
您的上下文爲空。發佈你如何打電話給這個課程,並在哪裏 – droidpl