android - 发布错误通知 - 无法展开RemoteViews:StatusBarNotification

问题描述 投票:40回答:12

我试图在IntentService的通知区域中发布带有自定义视图的通知,并获取Couldn't expect RemoteView错误。

这是我在onCreate()做的事情:

mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
icon = R.drawable.icon;
tickerText = "data upload in progress";
contentView = new RemoteViews(getApplicationContext().getPackageName(), R.layout.notiflayout);
contentView.setImageViewResource(R.id.image, R.drawable.icon);
contentView.setTextViewText(R.id.text, "Hello");
contentView.setProgressBar(R.id.progressBar, 100, 10, false);
whatNext = PendingIntent.getActivity(getApplicationContext(), 0, new Intent(getApplicationContext(), starterActivity.class), 0);
notification = new Notification(icon, tickerText, System.currentTimeMillis());
notification.contentView = contentView;
notification.contentIntent = whatNext;

我从notify()打电话给onHandleIntent(),取消onDestroy()的通知。

我已经验证此代码可以在一个独立的应用程序中运行,该应用程序没有IntentService。在IntentService这样做是某种程度上给麻烦。

有人可以解释一下我做错了什么吗?

谢谢!

java android android-service android-notifications
12个回答
14
投票

对我来说,问题是我在自定义通知视图xml文件中为根布局设置了特定高度。

我一改变了:

机器人:layout_height = “@扪/ notification_expanded”

机器人:layout_height = “match_parent”

在通知视图的根布局中,问题得以解决。

另外,请查看this example,了解使用自定义布局进行通知的简单示例。


0
投票

我在通知中显示自定义布局时面临同样的问题,我发现的是:

我使用ConstraintsLayout作为我的自定义通知的根布局,这是我提交的错误。因为在android中使用约束布局存在一些限制。

最后,我将我的根布局更改为RelativeLayout,我的通知显示完美。我在下面的截图中附上了我的观点。

enter image description here


0
投票

在我的情况下,问题是电话之间的不一致

setShowActionsInCompactView(0)

并且.addAction称...行动的数量不同,因此错误


-2
投票

通常,此错误表示您的contentView错误,请检查它!也许你最好用只包含TextView的布局替换你的contentView.Ok,运行它,希望对你有所帮助。


10
投票

在我的情况下,异常是由我的自定义通知布局中的常规View引起的。基本上,这是因为您只能使用TextView,ImageView等特定小部件。


10
投票

由于未知原因,您不能在自定义远程视图的根视图中引用维度!所以你必须硬编码像android:layout_height="64dp"但如果你使用android:layout_height="@dimen/notification_height_that_64"它会给你Bad notification posted - Couldn't expand RemoteViews for: StatusBarNotification。我希望这个能帮上忙 :)


6
投票

对我来说,问题是在自定义通知的自定义布局集中有一个View项。从布局中删除View项目解决了发布的错误通知的问题。

如果您想使用list of layout items创建自定义通知,可以使用RemoteView

无论是清洁项目还是将layout_height设为match_parent都不适合我。


5
投票

在我的情况下,我能够通过减少我提供给.setSmallIcon();的图标大小来修复此错误


3
投票

我有同样的问题。就我而言:

原因 - >我用于builder.setAction(R.drawable.icon,...)函数一个vectordrawable我也尝试从支持lib启用它们但没有任何效果。在最近的Android系统中,我没有看到动作图标,在其他系统中它会出现此错误。

解决方案 - >我没有找到任何结果,对我来说唯一的解决方法是避免使用.xml文件进行drawables并在所有目录中使用.png文件hdpi mdpi ldpi ..


3
投票

我得到了同样的错误,但对我来说问题是约束布局。我把它改成了Relative Layout以解决问题。


1
投票

@ Nikolai的回答对我有帮助,确实是问题所在。我遇到过同样的问题。可以在通知中使用特定控件。我的布局中有一个View用于通知,如下所示。

<View
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="0.04"/>

这导致了崩溃。支持以下布局和控件:enter image description here

按照这个official documentation

我删除它,它工作正常。希望它可以帮助某人。


0
投票

使用Vector drawable时要小心。在前Lollipop设备上通过NotificationCompat.Builder方法设置图标,如setSmallIcon,将导致此崩溃。如果在自定义视图中使用Vector drawable,您将会遇到同样的崩溃。

© www.soinside.com 2019 - 2024. All rights reserved.