2015-10-11 55 views
1
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); 
     } 
+0

您的上下文爲空。發佈你如何打電話給這個課程,並在哪裏 – droidpl

回答

7

在你的代碼中

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

contextnull

檢查你的堆棧跟蹤你的初始化AlarmTask

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

移動在onActivityCreated方法此調用。

1

您可以使用getActivity(),它會返回一個片段相關聯的活動。 所以,你的代碼將變爲:

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

你可以有一個NPE這個代碼。這取決於你何時調用getActivity()。當活動尚未定義時,您可以調用它。 –

4

如果要返回到的片段而不同的片段內部之中,並且在返回到在執行的方法中的片段,其是造成

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

問題就解決了。

相關問題