简体   繁体   English

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

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

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

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

In your code the 在你的代码中

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

the context is null . contextnull

Checking your stacktrace your are initializing the AlarmTask in 检查你的stacktrace你正在初始化AlarmTask

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

Move this call in the onActivityCreated method. onActivityCreated方法中移动此调用。

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. 问题将得到解决。

You can use getActivity() , which returns the activity associated with a fragment. 您可以使用getActivity() ,它返回与片段关联的活动。 So your code will become: 所以你的代码将成为:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 尝试在空对象引用上调用虚拟方法“java.lang.Object android.content.Context.getSystemService(java.lang.String)” - Attempt to invoke virtual method 'java.lang.Object android.content.Context.getSystemService(java.lang.String)' on a null object reference 尝试调用虚拟方法 &#39;java.lang.Object android.content.Context.getSystemService(java.lang.String) - Attempt to invoke virtual method 'java.lang.Object android.content.Context.getSystemService(java.lang.String) Android 应用程序返回“错误:尝试调用虚拟方法 &#39;java.lang.Object android.content.Context.getSystemService(java.lang.String)” - Android app returns "Error: Attempt to invoke virtual method 'java.lang.Object android.content.Context.getSystemService(java.lang.String)" ViewPagerAdapter尝试在空对象引用上调用虚拟方法Context.getSystemService(java.lang.String)&#39; - ViewPagerAdapter Attempt to invoke virtual method Context.getSystemService(java.lang.String)' on a null object reference android.content.Context.getSystemService(java.lang.String)&#39;对空对象的引用 - android.content.Context.getSystemService(java.lang.String)' on a null object reference Flutter:PlatformException(错误,尝试调用虚拟方法'java.lang.Object android.app.Activity.getSystemService(java.lang.String) - Flutter:PlatformException(error, Attempt to invoke virtual method 'java.lang.Object android.app.Activity.getSystemService(java.lang.String)' 尝试在 null object 引用错误上调用虚拟方法 'java.lang.String android.content.Context.getPackageName()' - Attempt to invoke virtual method 'java.lang.String android.content.Context.getPackageName()' on a null object reference error 尝试在空对象引用上调用虚拟方法 &#39;android.content.Context.getSharedPreferences(java.lang.String, int)&#39; - Attempt to invoke virtual method 'android.content.Context.getSharedPreferences(java.lang.String, int)' on a null object reference Android错误:尝试在空对象引用上调用虚拟方法&#39;java.lang.String android.content.Intent.getStringExtra(java.lang.String)&#39; - Android Error : Attempt to invoke virtual method 'java.lang.String android.content.Intent.getStringExtra(java.lang.String)' on a null object reference 尝试在null对象引用上调用虚拟方法&#39;java.lang.String [] android.os.Bundle.getStringArray(java.lang.String)&#39; - Attempt to invoke virtual method 'java.lang.String[] android.os.Bundle.getStringArray(java.lang.String)' on a null object reference
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM