问题
I am trying to post a notification with a custom view in the notification area from an IntentService
, and getting the Couldn't expand 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:
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.
回答2:
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 :)
回答3:
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.
回答4:
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.
回答5:
In my case, i was able to fix this error by reducing the icon size i was providing into .setSmallIcon();
回答6:
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..
回答7:
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.
回答8:
@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. Following Layouts and controls are supported:
As per this official documentation.
I removed it and it worked fine. Hope it helps someone.
回答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:
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
回答12:
i see the is alot of trouble related to this topic, in my case this problem was caused by using
android:background="?attr/selectableItemBackground"
i used that on my ImageButton
and each time my app crashed but when i removed it, everything was fine, i guess the problem is you should not use any custom View
type or any theme attributes on your views, i hope this will help another stuck with this problem
回答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.
来源:https://stackoverflow.com/questions/6209631/bad-notification-posted-couldnt-expand-remoteviews-for-statusbarnotification