"Unable to create service" 闹钟设置错误

"Unable to create service" error on alarm set

我在尝试设置闹钟时遇到问题:

我的服务 "SetNotifications" 由名为 "BroadcastReceiverCalendar" 的广播接收器调用。 服务"SetNotifications"设置了多个闹钟。

我的应用程序在最后一行崩溃,请参阅下面的错误代码。

这是我的闹钟设置代码:

Intent alarmIntent = new Intent(this, BroadcastReceiverCalendar.class);
String action = "ServiceCalendrier.ALARM_NOTIF_"+i;
alarmIntent.setAction(action);
alarmIntent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND);
alarmIntent.putExtra ("COURS", cours);
alarmIntent.putExtra ("SALLE", salle);
final PendingIntent mPendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, alarmIntent, 0); 
manager.set(AlarmManager.RTC_WAKEUP, time.getTimeInMillis(), mPendingIntent); // crash here

错误输出:

03-11 14:59:29.646  15021-15021/com.example.xx W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x40c4c1f8)
03-11 14:59:29.656  15021-15021/com.example.xx E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.RuntimeException: Unable to create service com.example.xx.ServiceSetNotifications: java.lang.NullPointerException
            at android.app.ActivityThread.handleCreateService(ActivityThread.java:2263)
            at android.app.ActivityThread.access00(ActivityThread.java:123)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1201)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:4424)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:787)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:554)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.NullPointerException
            at com.example.xx.ServiceSetNotifications.setNotificationsAlarms(ServiceSetNotifications.java:141)
            at com.example.xx.ServiceSetNotifications.onCreate(ServiceSetNotifications.java:61)
            at android.app.ActivityThread.handleCreateService(ActivityThread.java:2253)
            at android.app.ActivityThread.access00(ActivityThread.java:123)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1201)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:4424)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:787)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:554)
            at dalvik.system.NativeStart.main(Native Method)

我尝试了几种设置 pendingIntent 的方法,但我无法让它工作。 你有什么主意吗? 提前致谢!

更新

public class ServiceSetNotifications extends Service{

    BroadcastReceiverCalendar receiver  = new BroadcastReceiverCalendar();
    private static final String TAG = "Servicesetnotif";
    private PendingIntent pendingIntent;
    private AlarmManager manager;


    @Override
    public IBinder onBind(Intent intent) {
        //TODO for communication return IBinder implementation
        return null;
    }
    public void onDestroy() {
        super.onDestroy();
        //unregisterReceiver(receiver);
        Toast.makeText(this, "service notif", Toast.LENGTH_LONG).show();
        Log.d(TAG, "onDestroy");
    }

    @Override
    public void onCreate() {
        super.onCreate();
        Log.v("Sortie","Service setNotification");
       // getIcs(getApplicationContext());
        //Context context = getApplicationContext();
        setNotificationsAlarms(getApplicationContext());

    }
   @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
       //Context context = getApplicationContext();
       setNotificationsAlarms(getApplicationContext());

        return START_NOT_STICKY;
    }

    public void setNotificationsAlarms(Context arg0){

        SharedPreferences mPrefs = getApplicationContext().getSharedPreferences("label", 0);
        int notifTime = mPrefs.getInt("notifTime", 10);
        //Log.v("Sortie",Integer.toString(notifTime));
        List coursRaw = new ArrayList();
        List<String[]> listeCours = new ArrayList<String[]>();



        //analyse of ics file
        listeCours=parseFile();

        Log.v("Sortie","Parse done");

        if(listeCours.isEmpty()){
            Log.v("Sortie","holidays");
        }
        else{
            for (int i = listeCours.size()-1; i >= 0 ; i--) {

                String[] strings = listeCours.get(i);
                String cours = strings[0];
                String heure = strings[1];
                String salle = strings[2];

                Log.v("Sortie",cours+" "+salle+" "+i);
                DateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
                Calendar time = defTime(heure, notifTime);
                //UTC to Paris time
                dateFormat.setCalendar(time);
                dateFormat.setTimeZone(TimeZone.getTimeZone("Europe/Paris"));

                //Log.v("Sortie","!Heure paris :"+time.get(Calendar.HOUR_OF_DAY) + ":" + time.get(Calendar.MINUTE));

                //IntentFilter myFilter = new IntentFilter("ServiceCalendrier.ALARM_NOTIF_"+i);
                Intent alarmIntent = new Intent(this, BroadcastReceiverCalendar.class);
                String action = "ServiceCalendrier.ALARM_NOTIF_"+i;
                alarmIntent.setAction(action);

                alarmIntent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND);
                alarmIntent.putExtra ("COURS", cours);
                alarmIntent.putExtra ("SALLE", salle);


                manager.set(AlarmManager.RTC_WAKEUP, time.getTimeInMillis(), mPendingIntent);

            }


        }

    //Log.v("Sortie","parse done");
}

}

您没有在您的服务中实例化 AlarmManager。

在您的服务的 onCreate 方法中添加:

manager=(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);