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.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);
}
答案 0 :(得分:11)
在你的代码中
this.am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
context
为null
。
检查您的堆栈跟踪是否正在初始化
中的AlarmTask
at lol.com.epl.FixFragment.onCreateView(FixFragment.java:88)
使用onActivityCreated
方法移动此调用。
答案 1 :(得分:5)
如果你在一个不同的片段中返回片段,并且在返回片段时你执行一个导致
的方法java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object android.content.Context.getSystemService(java.lang.String)' on a null object reference
在
中包装该方法if (getActivity() != null) {
// Code goes here.
}
问题将得到解决。
答案 2 :(得分:3)
您可以使用getActivity()
,它返回与片段关联的活动。
所以你的代码将成为:
public AlarmTask(Calendar date) {
this.am = (AlarmManager)getActivity().getSystemService(Context.ALARM_SERVICE); //error for this line
this.date = date;
}