我在Intent alarmIntent = new Intent(this, MyNotification.class);
获得了一个nullpointer但是我不知道如何解决这个问题。我希望有人可以帮助我..
它应该做的是每天将警报设置为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);
}
}
Logcat输出:
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
答案 0 :(得分:1)
只是想知道,为什么你这样做,而不是仅仅将setRecurringAlarm()方法放在你的MainActivity中?
您只是创建Alarm活动的实例,并在其上调用方法。我真的不认为这需要额外的活动。
只是猜测,但可能使用Alarm活动作为上下文而没有实际正确启动它可能导致NullPointerException。