android - 使用 BroadcastReceiver 实现 Android 通知操作时出错
问题描述
我正在尝试使通知中的操作按钮调用一种方法。我阅读并关注了一些文章,但是在我的应用程序中发出通知后(应用程序本身编译没有问题),它崩溃并出现以下错误:
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
这是我的简化代码:
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!")
}
}
解决方案
问题是我需要在某些方法(startForeground()
在我的情况下)中初始化我的意图,而不是在类初始化程序中,如下所示:
class Stopwatch : Service() {
private fun startForeground() {
...
val intent = Intent(this, ActionReceiver::class.java).apply {
action = "snooze"
}
val pendingIntent: PendingIntent = PendingIntent.getBroadcast(this, 0, snoozeIntent, 0)
builder
...
.addAction(R.drawable.ic_round_play_arrow_24, "Play", pendingIntent)
}
}
推荐阅读
- sql - 根据另一个 id 列更新具有(列组值的 max+1)列的脚本
- scheduled-tasks - PowerShell 计划任务失败并显示错误代码:0xFFFD0000 和 4294770688 如果每天每小时运行一次
- c# - 将 8 个点转换为矩形
- flutter - 如何在 Stream 方法中转换 Future 方法以提供列表
- node.js - Vonage 文本转语音出站电话的 NodeJS 示例代码?
- java - Infinispan 9, Replicated Cache is Expiring Entries 但绝不允许将它们从 JVM 堆中删除
- dependencies - 依赖倒置 (DI) 中短语“应该依赖于抽象”的解释
- xml - 将 XML 读入列表(对象)
- html - Css 和 bootstrap 在 chrome 中不起作用,但在实时服务器中起作用
- swift - SwiftUI - 我的列表并没有一直到我的导航视图