android.content.contextwrapper.getsystemservice上的java.lang.nullpointerexception(Contextwrapper.java:386)

时间:2013-02-06 14:46:57

标签: java android

我正在使用自定义日期和时间创建通知服务,但我无法执行此操作。 我下载了这段代码http://blog.blundell-apps.com/notification-for-a-user-chosen-time/,这个工作正常,但是当我在项目中包含代码时,它不起作用。

我有这段代码

package com.blundell.tut.service.task;

import java.util.Calendar;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

import com.blundell.tut.service.NotifyService;

/**
 * Set an alarm for the date passed into the constructor
 * When the alarm is raised it will start the NotifyService
 * 
 * This uses the android build in alarm manager *NOTE* if the phone is turned off this alarm will be cancelled
 * 
 * This will run on it's own thread.
 * 
 * @author paul.blundell
 */
public class AlarmTask implements Runnable{
    // The date selected for the alarm
    private final Calendar date;
    // The android system alarm manager
    AlarmManager am;
    // Your context to retrieve the alarm manager from
    private final Context contextt;

    public AlarmTask(Context context, Calendar date) {
        Log.v("AlarmTask", "AlarmTask");
        this.contextt = context;
        this.am = (AlarmManager) context.getSystemService(contextt.ALARM_SERVICE);
        this.date = date;
    }

    public void run() {
        Log.v("AlarmTask", "run");
        // Request to start are service when the alarm date is upon us
        // We don't start an activity as we just want to pop up a notification into the system bar not a full activity
        Intent intent = new Intent(contextt, NotifyService.class);
        intent.putExtra(NotifyService.INTENT_NOTIFY, true);
        PendingIntent pendingIntent = PendingIntent.getService(contextt, 0, intent, 0);

        // Sets an alarm - note this alarm will be lost if the phone is turned off and on again
        am.set(AlarmManager.RTC, date.getTimeInMillis(), pendingIntent);
    }
}

在这一行中我收到错误

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

我从这个班级发送de context

package com.blundell.tut.service;

import java.util.Calendar;

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;

import com.blundell.tut.service.task.AlarmTask;

public class ScheduleService extends Service {

    /**
     * Class for clients to access
     */
    public class ServiceBinder extends Binder {
        ScheduleService getService() {
            return ScheduleService.this;
        }
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        return START_STICKY;
    }

    @Override
    public IBinder onBind(Intent intent) {
        return mBinder;
    }

    // This is the object that receives interactions from clients. See
    private final IBinder mBinder = new ServiceBinder();

    /**
     * Show an alarm for a certain date when the alarm is called it will pop up a notification
     */
    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();
    }
}

我收到此错误

java.lang.nullpointerexception 
   at android.content.contextwrapper.getsystemservice(Contextwrapper.java:386)

对不起我的英文:)

2 个答案:

答案 0 :(得分:0)

如果你想要的只是在将来的某个时间点发出通知,那么您只需使用AlarmManager,设置BroadcastReceiver并从IntentService开始这一点。

以下是一些代码段:

AlarmManager Not called next day if the app sleeps for abt 1 day

答案 1 :(得分:0)

在这一行:

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

您应该以静态方式访问ALARM_SERVICE,请尝试以下方法:

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

此外,您的通话环境应该可以通过this.getBaseContext()this.getApplicationContext

访问

即。你可以写:

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