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

Cha*_*nya 43 java android android-service android-notifications

我试图从IntentService在通知区域中发布带有自定义视图的通知,并获取IntentService错误.

这是我在做的事情Couldn't expand RemoteView:

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;
Run Code Online (Sandbox Code Playgroud)

我打电话onCreate()notify(),并取消通知onHandleIntent().

我已经验证此代码可以在一个独立的应用程序中运行,该应用程序没有onDestroy().这样做IntentService是以某种方式给予麻烦.

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

谢谢!

Ima*_*ari 15

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

我一改变了:

机器人:layout_height = "@扪/ notification_expanded"

机器人:layout_height = "match_parent"

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

另请参阅此示例,以查看使用自定义布局进行通知的简单示例.


小智 12

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


Nik*_*lai 10

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


Rea*_*hed 6

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

如果要使用创建自定义通知,可以使用以下列表RemoteView.

无论是清洁项目也设置layout_heightmatch_parent为我工作.


Gha*_*dra 5

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