Error while implementing Android Notification Actions with BroadcastReceiver

I am trying to make action buttons in my notification call a method. I read and follow some articles, but after laucnhing a notification in my app (the app itself compiles withought a problem), it crashes with a following error:

D/AndroidRuntime(28351): Shutting down VM
E/AndroidRuntime(28351): FATAL EXCEPTION: main
E/AndroidRuntime(28351): Process: com.example.tasko, PID: 28351
E/AndroidRuntime(28351): java.lang.RuntimeException: Unable to instantiate service com.example.tasko.Stopwatch: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Context.getPackageName()' on a null object reference
E/AndroidRuntime(28351):    at android.app.ActivityThread.handleCreateService(ActivityThread.java:3940)
E/AndroidRuntime(28351):    at android.app.ActivityThread.access$1500(ActivityThread.java:219)
E/AndroidRuntime(28351):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1875)
E/AndroidRuntime(28351):    at android.os.Handler.dispatchMessage(Handler.java:107)
E/AndroidRuntime(28351):    at android.os.Looper.loop(Looper.java:214)
E/AndroidRuntime(28351):    at android.app.ActivityThread.main(ActivityThread.java:7356)
E/AndroidRuntime(28351):    at java.lang.reflect.Method.invoke(Native Method)
E/AndroidRuntime(28351):    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
E/AndroidRuntime(28351):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
E/AndroidRuntime(28351): Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Context.getPackageName()' on a null object reference
E/AndroidRuntime(28351):    at android.content.ContextWrapper.getPackageName(ContextWrapper.java:145)
E/AndroidRuntime(28351):    at android.content.ComponentName.<init>(ComponentName.java:131)
E/AndroidRuntime(28351):    at android.content.Intent.<init>(Intent.java:6510)
E/AndroidRuntime(28351):    at com.example.tasko.Stopwatch.<init>(StopwatchService.kt:21)
E/AndroidRuntime(28351):    at java.lang.Class.newInstance(Native Method)
E/AndroidRuntime(28351):    at android.app.AppComponentFactory.instantiateService(AppComponentFactory.java:129)
E/AndroidRuntime(28351):    at androidx.core.app.CoreComponentFactory.instantiateService(CoreComponentFactory.java:75)
E/AndroidRuntime(28351):    at android.app.ActivityThread.handleCreateService(ActivityThread.java:3935)
E/AndroidRuntime(28351):    ... 8 more

Here is the simplified code of mine:

class Stopwatch : Service() {
    // error at this line
    val intent = Intent(this, ActionReceiver::class.java).apply {
        action = "snooze"
    }
    val pendingIntent: PendingIntent = PendingIntent.getBroadcast(this, 0, snoozeIntent, 0)

    private fun startForeground() {
        ...
        builder
            ...
            .addAction(R.drawable.ic_round_play_arrow_24, "Play", pendingIntent)
    }
}

ActionReceiver.kt:

class ActionReceiver : BroadcastReceiver() {
    override fun onReceive(context: Context?, intent: Intent?) {
        println("Hello!")
    }
}


Read more here: https://stackoverflow.com/questions/68466073/error-while-implementing-android-notification-actions-with-broadcastreceiver

Content Attribution

This content was originally published by John at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: