RemoteServiceException 在 MIUI 11 上使我的应用程序崩溃

问题描述

我开始收到来自运行 Android 11 的 MIUI 11 设备的奇怪崩溃(到目前为止只有 Mi 10 和 Mi 10 lite 5G)。我认为这是一个平台问题,我的应用程序中没有任何内容,因为它非常特定于小米 Android 11。

Fatal Exception: android.app.RemoteServiceException
Bad notification(tag=null,id=3249) posted from package de.crysxd.octoapp,crashing app(uid=10334,pid=23788): Couldn't inflate contentViewsjava.lang.NullPointerException: Attempt to invoke virtual method 'android.app.Notification$MessagingStyle android.app.Notification$MessagingStyle.setConversationType(int)' on a null object reference

我知道如果您例如,可能会发生类似的崩溃。在旧设备上使用 SVG 图标,但我已经使用 PNG。设备只显示两种类型的通知,一种是前台服务,一种来自 Firebase。从崩溃的时间来看,似乎不太可能是 Firebase 通知。

这是我用来创建通知 (here in full) 的代码:

private fun createProgressNotification(progress: Int,title: String,status: String) = createNotificationBuilder()
        .setContentTitle(title)
        .setContentText(status)
        .setProgress(maxProgress,progress,false)
        .setOngoing(true)
        .addCloseAction()
        .setNotificationSilent()
        .build()

    private fun createCompletedNotification(name: String?) = createNotificationBuilder()
        .setContentTitle(getString(R.string.notification_print_done_title))
        .apply {
            name?.let {
                setContentText(it)
            }
        }
        .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
        .build()

    private fun createDisconnectedNotification() = createNotificationBuilder()
        .setContentTitle(getString(R.string.notification_printing_lost_connection_message))
        .setContentText(lastEta)
        .setProgress(maxProgress,true)
        .addCloseAction()
        .setOngoing(false)
        .setNotificationSilent()
        .build()

    private fun createInitialNotification() = createNotificationBuilder()
        .setContentTitle(getString(R.string.notification_printing_title))
        .setProgress(maxProgress,true)
        .setOngoing(true)
        .addCloseAction()
        .setNotificationSilent()
        .build()

    private fun createNotificationBuilder() = NotificationCompat.Builder(this,notificationChannelId)
        .setColorized(true)
        .setColor(ContextCompat.getColor(this,R.color.primary_light))
        .setSmallIcon(R.drawable.ic_notification_default)
        .setContentIntent(createStartAppPendingIntent())

有人遇到同样的问题或知道解决方案吗?

解决方法

我以前的答案是隐藏的。 将系统更新到MIUI 12.2.7,然后崩溃。 我的应用程序中有每秒更新的通知,并且崩溃仅在应用程序持续运行一段时间后才会发生。

,

查看错误,应用程序因空指针异常而崩溃。

Fatal Exception: android.app.RemoteServiceException
Bad notification(tag=null,id=3249) posted from package de.crysxd.octoapp,crashing app(uid=10334,pid=23788): 

Couldn't inflate contentViewsjava.lang.NullPointerException: Attempt to invoke virtual method 'android.app.Notification$MessagingStyle android.app.Notification$MessagingStyle.setConversationType(int)' 
on a null object reference

我建议您使用 Log 类来找出错误发生在哪一行。

现在,您可以将通知代码包装在 try... catch 块中,并在 catch 块中记录错误。这将防止您的应用崩溃,这更好。

这种方法将帮助您更好地了解问题并修复它,或者至少使其具有一些有限的功能,这比使应用崩溃要好。

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...