문제

I get a nullpointer at Intent alarmIntent = new Intent(this, MyNotification.class); But I have no idea how to fix this.. I hope someone might be able to help me..

What it should do is set the alarm everyday at 9 so I can set a notification in the MyNotification class which contains a BroadcastReceiver.

Below is all my code:

Calling the AlarmManager class (From MainActivity):

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

The class where I want to set the 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 output:

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
도움이 되었습니까?

해결책

Just wondering, why are you doing it this way, instead of just putting the setRecurringAlarm() method in your MainActivity?

You are just creating an instance of your Alarm activity, and calling a method on it. I dont really see the need for this to be in an extra activity.

Just guessing, but probably using the Alarm activity as a context without it actually being properly launched could be causing the NullPointerException.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top