1

我得到一个空指针Intent alarmIntent = new Intent(this, MyNotification.class);但我不知道如何解决这个问题..我希望有人能帮助我..

它应该做的是每天在 9 点设置闹钟,这样我就可以在包含 BroadcastReceiver 的 MyNotification 类中设置通知。

以下是我的所有代码:

调用 AlarmManager 类(来自 MainActivity):

Alarm setAlarm = new Alarm();
        setAlarm.setRecurringAlarm();

我要设置 AlarmManager 的类(Alarm.class):

public class Alarm extends Activity {
public void setRecurringAlarm() {

    Log.i("Alarm", "Setting Recurring Alarm");

    Calendar updateTime = Calendar.getInstance();

    updateTime.set(Calendar.HOUR, 7);
    updateTime.set(Calendar.MINUTE, 0);
    updateTime.set(Calendar.SECOND, 0);

    Intent alarmIntent = new Intent(this, MyNotification.class);
    PendingIntent recurringDownload = PendingIntent.getBroadcast(this, 0, alarmIntent, PendingIntent.FLAG_CANCEL_CURRENT);
    AlarmManager alarms = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    alarms.cancel(recurringDownload);
    alarms.setInexactRepeating(AlarmManager.RTC, updateTime.getTimeInMillis(), AlarmManager.INTERVAL_DAY , recurringDownload);
}

}

日志输出:

08-07 22:43:38.079: E/AndroidRuntime(21476): FATAL EXCEPTION: main
08-07 22:43:38.079: E/AndroidRuntime(21476): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.weatherclothes/com.weatherclothes.MainActivity}: java.lang.NullPointerException
08-07 22:43:38.079: E/AndroidRuntime(21476):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2355)
08-07 22:43:38.079: E/AndroidRuntime(21476):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2391)
08-07 22:43:38.079: E/AndroidRuntime(21476):    at android.app.ActivityThread.access$600(ActivityThread.java:151)
08-07 22:43:38.079: E/AndroidRuntime(21476):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1335)
08-07 22:43:38.079: E/AndroidRuntime(21476):    at android.os.Handler.dispatchMessage(Handler.java:99)
08-07 22:43:38.079: E/AndroidRuntime(21476):    at android.os.Looper.loop(Looper.java:155)
08-07 22:43:38.079: E/AndroidRuntime(21476):    at android.app.ActivityThread.main(ActivityThread.java:5493)
08-07 22:43:38.079: E/AndroidRuntime(21476):    at java.lang.reflect.Method.invokeNative(Native Method)
08-07 22:43:38.079: E/AndroidRuntime(21476):    at java.lang.reflect.Method.invoke(Method.java:511)
08-07 22:43:38.079: E/AndroidRuntime(21476):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1028)
08-07 22:43:38.079: E/AndroidRuntime(21476):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:795)
08-07 22:43:38.079: E/AndroidRuntime(21476):    at dalvik.system.NativeStart.main(Native Method)
08-07 22:43:38.079: E/AndroidRuntime(21476): Caused by: java.lang.NullPointerException
08-07 22:43:38.079: E/AndroidRuntime(21476):    at android.content.ComponentName.<init>(ComponentName.java:75)
08-07 22:43:38.079: E/AndroidRuntime(21476):    at android.content.Intent.<init>(Intent.java:3655)
08-07 22:43:38.079: E/AndroidRuntime(21476):    at com.weatherclothes.Alarm.setRecurringAlarm(Alarm.java:23)
08-07 22:43:38.079: E/AndroidRuntime(21476):    at com.weatherclothes.MainActivity.onCreate(MainActivity.java:58)
08-07 22:43:38.079: E/AndroidRuntime(21476):    at android.app.Activity.performCreate(Activity.java:5066)
08-07 22:43:38.079: E/AndroidRuntime(21476):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1101)
08-07 22:43:38.079: E/AndroidRuntime(21476):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2311)
08-07 22:43:38.079: E/AndroidRuntime(21476):    ... 11 more
4

1 回答 1

1

只是想知道,你为什么要这样做,而不是把 setRecurringAlarm() 方法放在你的 MainActivity 中?

您只是在创建警报活动的实例,并在其上调用方法。我真的不认为有必要进行额外的活动。

只是猜测,但可能将警报活动用作上下文而没有实际正确启动可能会导致 NullPointerException。

于 2013-08-08T00:43:30.720 回答