使用 BroadcastReceiver 实现 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,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,snoozeIntent,0)
        builder
            ...
            .addAction(R.drawable.ic_round_play_arrow_24,"Play",pendingIntent)
    }
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?
Java在半透明框架/面板/组件上重新绘画。
Java“ Class.forName()”和“ Class.forName()。newInstance()”之间有什么区别?
在此环境中不提供编译器。也许是在JRE而不是JDK上运行?
Java用相同的方法在一个类中实现两个接口。哪种接口方法被覆盖?
Java 什么是Runtime.getRuntime()。totalMemory()和freeMemory()?
java.library.path中的java.lang.UnsatisfiedLinkError否*****。dll
JavaFX“位置是必需的。” 即使在同一包装中
Java 导入两个具有相同名称的类。怎么处理?
Java 是否应该在HttpServletResponse.getOutputStream()/。getWriter()上调用.close()?
Java RegEx元字符(。)和普通点?