Simple-Alarms icon indicating copy to clipboard operation
Simple-Alarms copied to clipboard

Unable to start alarm receiver (emulator API 24)

Open AqilaKarim opened this issue 8 years ago • 2 comments

I am trying to use this for my personal project in android studio. I am using a Genymotion emulator (google nexus phone API 24) but the app crashed everytime the alarm is about to pop. here is the message from the logcat:

--------- beginning of crash 05-10 01:44:16.380 1596-1596/com.github.ppartisan.simplealarms E/AndroidRuntime: FATAL EXCEPTION: main Process: com.github.ppartisan.simplealarms, PID: 1596 java.lang.RuntimeException: Unable to start receiver com.github.ppartisan.simplealarms.service.AlarmReceiver: java.lang.NullPointerException: Attempt to invoke virtual method 'long com.github.ppartisan.simplealarms.model.Alarm.getId()' on a null object reference at android.app.ActivityThread.handleReceiver(ActivityThread.java:3018) at android.app.ActivityThread.-wrap18(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1544) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:154) at android.app.ActivityThread.main(ActivityThread.java:6077) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755) Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'long com.github.ppartisan.simplealarms.model.Alarm.getId()' on a null object reference at com.github.ppartisan.simplealarms.util.AlarmUtils.getNotificationId(AlarmUtils.java:155) at com.github.ppartisan.simplealarms.service.AlarmReceiver.onReceive(AlarmReceiver.java:31) at android.app.ActivityThread.handleReceiver(ActivityThread.java:3011) at android.app.ActivityThread.-wrap18(ActivityThread.java)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1544)  at android.os.Handler.dispatchMessage(Handler.java:102)  at android.os.Looper.loop(Looper.java:154)  at android.app.ActivityThread.main(ActivityThread.java:6077)  at java.lang.reflect.Method.invoke(Native Method)  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755) 

AqilaKarim avatar May 10 '18 05:05 AqilaKarim

you found the error?

JMVasquezR avatar Jun 25 '18 16:06 JMVasquezR

I was able to replicate this issue on Android N, and it looks like the cause is covered this SO post.

A solution that worked for me involved following the advice in the accepted answer on that page and wrapping the Alarm inside a Bundle, so inside AlarmReceiver swap:

public final class AlarmReceiver extends BroadcastReceiver {

    private static final String ALARM_EXTRA = "alarm_extra";

    @Override
    public void onReceive(Context context, Intent intent) {

        final Alarm alarm = intent.getParcelable(ALARM_EXTRA);
        final int id = AlarmUtils.getNotificationId(alarm);
        //...
    }

    public static void setReminderAlarm(Context context, Alarm alarm) {
        //...
        final Intent intent = new Intent(context, AlarmReceiver.class);
        bundle.putParcelable(ALARM_EXTRA, alarm);
        //...
    }

With:

public final class AlarmReceiver extends BroadcastReceiver {

    private static final String BUNDLE_EXTRA = "bundle_extra";
    private static final String ALARM_KEY = "alarm_key";

    @Override
    public void onReceive(Context context, Intent intent) {

        final Alarm alarm = intent.getBundleExtra(BUNDLE_EXTRA).getParcelable(ALARM_KEY);
        final int id = AlarmUtils.getNotificationId(alarm);
        //...
    }

    public static void setReminderAlarm(Context context, Alarm alarm) {
        //...
        final Intent intent = new Intent(context, AlarmReceiver.class);
        final Bundle bundle = new Bundle();
        bundle.putParcelable(ALARM_KEY, alarm);
        intent.putExtra(BUNDLE_EXTRA, bundle);
        //...
    }

I might put out a fix for this eventually, but if not then the above solved the issue on my device.

PPartisan avatar Jun 30 '18 18:06 PPartisan