我试图在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
这样做是某种程度上给麻烦。
有人可以解释一下我做错了什么吗?
谢谢!
对我来说,问题是我在自定义通知视图xml文件中为根布局设置了特定高度。
我一改变了:
机器人:layout_height = “@扪/ notification_expanded”
至
机器人:layout_height = “match_parent”
在通知视图的根布局中,问题得以解决。
另外,请查看this example,了解使用自定义布局进行通知的简单示例。
在我的情况下,问题是电话之间的不一致
setShowActionsInCompactView(0)
并且.addAction
称...行动的数量不同,因此错误
通常,此错误表示您的contentView错误,请检查它!也许你最好用只包含TextView的布局替换你的contentView.Ok,运行它,希望对你有所帮助。
在我的情况下,异常是由我的自定义通知布局中的常规View
引起的。基本上,这是因为您只能使用TextView,ImageView等特定小部件。
由于未知原因,您不能在自定义远程视图的根视图中引用维度!所以你必须硬编码像android:layout_height="64dp"
但如果你使用android:layout_height="@dimen/notification_height_that_64"
它会给你Bad notification posted - Couldn't expand RemoteViews for: StatusBarNotification
。我希望这个能帮上忙 :)
对我来说,问题是在自定义通知的自定义布局集中有一个View
项。从布局中删除View
项目解决了发布的错误通知的问题。
如果您想使用list of layout items创建自定义通知,可以使用RemoteView
。
无论是清洁项目还是将layout_height
设为match_parent
都不适合我。
在我的情况下,我能够通过减少我提供给.setSmallIcon();
的图标大小来修复此错误
我有同样的问题。就我而言:
原因 - >我用于builder.setAction(R.drawable.icon,...)函数一个vectordrawable我也尝试从支持lib启用它们但没有任何效果。在最近的Android系统中,我没有看到动作图标,在其他系统中它会出现此错误。
解决方案 - >我没有找到任何结果,对我来说唯一的解决方法是避免使用.xml文件进行drawables并在所有目录中使用.png文件hdpi mdpi ldpi ..
我得到了同样的错误,但对我来说问题是约束布局。我把它改成了Relative Layout
以解决问题。
@ Nikolai的回答对我有帮助,确实是问题所在。我遇到过同样的问题。可以在通知中使用特定控件。我的布局中有一个View用于通知,如下所示。
<View
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.04"/>
我删除它,它工作正常。希望它可以帮助某人。
使用Vector drawable时要小心。在前Lollipop设备上通过NotificationCompat.Builder
方法设置图标,如setSmallIcon
,将导致此崩溃。如果在自定义视图中使用Vector drawable,您将会遇到同样的崩溃。