错误:尝试在空对象引用上调用虚拟方法“java.lang.Object android.content.Context.getSystemService(java.lang.String)”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33068530/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 14:16:02  来源:igfitidea点击:

error:Attempt to invoke virtual method 'java.lang.Object android.content.Context.getSystemService(java.lang.String)' on a null object reference

javaandroid

提问by

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;
}

There are similar questions, but after banging my head I couldn't sort out the problem. I am getting these following errors! Is it due to fragments?

有类似的问题,但敲了敲脑袋后,我无法解决问题。我收到以下错误!是因为碎片吗?

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.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)

This is how I am calling the class

这就是我打电话给班级的方式

 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();
}

This is where I call the setAlarm function

这是我调用 setAlarm 函数的地方

  public void setAlarmForNotification(Calendar c){
    ScheduleService mBoundService = new ScheduleService();
    mBoundService.setAlarm(c);
         }

采纳答案by Gabriele Mariotti

In your code the

在您的代码中

this.am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);

the contextis null.

contextnull

Checking your stacktrace your are initializing the AlarmTaskin

检查你的堆栈跟踪你的初始化AlarmTask

at lol.com.epl.FixFragment.onCreateView(FixFragment.java:88)

Move this call in the onActivityCreatedmethod.

onActivityCreated方法中移动这个调用。

回答by Zain Zafar

You can use getActivity(), which returns the activity associated with a fragment. So your code will become:

您可以使用getActivity(),它返回与片段关联的活动。所以你的代码将变成:

public AlarmTask(Calendar date) {
this.am = (AlarmManager)getActivity().getSystemService(Context.ALARM_SERVICE); //error for this line
this.date = date;
}

回答by Udit Kapahi

If you are returning to a fragment while being inside a different fragment, and upon returning to the fragment you execute a method which is causing the

如果您在不同片段内返回片段,并且在返回片段时执行导致

java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object android.content.Context.getSystemService(java.lang.String)' on a null object reference

Wrapped that method in a

将该方法包装在一个

if (getActivity() != null) {
// Code goes here.
}

and problem will be solved.

问题将得到解决。