可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I am trying to post a notification with custom view in the notification area from an IntentService, and getting the 'Couldn't expant RemoteView' error.
Here's what I am doing in 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;
I am calling notify() from OnHandleIntent(), and canceling the notifications in OnDestroy().
I have verified that this code works in an independent app, which does not have an IntentService. Doing this in an IntentService is somehow giving trouble.
Could someone please explain what is it that I am doing wrong?
Thanks!
回答1:
Well the error was due to ResourceNotFound Exception. I was getting the 'couldn't inflate view for notification' message.
I kept digging, and following the advice here fixed my problem. I renamed drawable-mdpi to drawable.
AND, after making sure that the code worked, I changed the folder name back to drawable-mdpi. Things are still working, making me believe that a 'Clean Project' followed by Build would have fixed the problem.
Hope this helps.
回答2:
For me, the problem was that I was setting a specific height for the root layout, in the custom notification view xml file.
As soon as I changed:
android:layout_height="@dimen/notification_expanded"
to
android:layout_height="match_parent"
in the root layout of notification view, the problem was solved.
Also take a look at this example to see a simple example of using custom layout for notifications.
回答3:
for unknown reason you are not allowed to reference dimention in the root view of the custom remote view! so you have to hard code it like android:layout_height="64dp"
but if you used android:layout_height="@dimen/notification_height_that_64"
it will give you Bad notification posted - Couldn't expand RemoteViews for: StatusBarNotification
. i hope this will help :)
回答4:
In my case the exception was caused by a regular View
in my custom notification layout. Basically, it's because you're allowed to use only certain widgets like TextView, ImageView and so on.
回答5:
For me the problem was having a View
item in the custom layout set for the custom notification. Removing the View
item from the layout solved the problem of the Bad Notification Posted.
Here's a list of layout items which can be used, if you want to create a custom notification using RemoteView
.
Neither cleaning project nor setting the layout_height
as match_parent
worked for me.
回答6:
In my case, i was able to fix this error by reducing the icon size i was providing into .setSmallIcon();
回答7:
I had the same problem. In my case:
reason -> I used for the builder.setAction(R.drawable.icon,...) function a vectordrawable and I tried also to enable them from support lib but nothing worked. In recent Android systems I do not see action icons, in the others it gives this error.
solution -> I did not find nothing, the only workaround for me is to avoid .xml files for drawables and to use the .png files in all directories hdpi mdpi ldpi..
回答8:
I got the same error, but the problem for me was the constrain layout. I changed it to Relative Layout
to fix the the problem.
回答9:
Be careful when using Vector drawables. On pre-Lollipop devices setting an icon through NotificationCompat.Builder
methods, like setSmallIcon
, will cause this crash. You'll get same crash if using Vector drawables in your custom view.
回答10:
I was facing same issue in showing custom layout in notification and what i found is:
I was using ConstraintsLayout as a root layout of my custom notification, this is the mistake i was committing. As there are some limitations with constrain layout to be used in android.
Finally i changed my root layout to RelativeLayout and my notification showing perfectly. I have attached my view in below screenshot.
回答11:
@Nikolai's answer was helpful for me, indeed it was the problem.
I had the same issue. There are specific controls that can be used in the notification. I had a View in my layout for notification as below.
<View
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.04"/>
This was causing the crash. I removed it and it worked fine. Hope it helps someone.
回答12:
In my case, the problem was an inconsistency between the call
setShowActionsInCompactView(0)
And the .addAction
calls...The number of actions was different, hence the error
回答13:
Generally,this error means your contentView is error,check it !
maybe you'd better replace your contentView with a layout that contain a TextView only.Ok,run it,hope help for you.