解决使用Notification自定义布局时出现android.app.RemoteServiceException Bad notification posted异常的问题

在使用Notification自定义布局时,代码实现为:

notification.contentView = new RemoteViews(mContext.getPackageName(), R.layout.content_view);

如果app运行到这行代码时出现如下错误:
android.app.RemoteServiceException Bad notification posted from package com.mypackage.test: Couldn't expand RemoteViews for: StatusBarNotification(pkg=com.mypackage.test user=UserHandle{0} id=-1314 tag=null score=0 key=0|com.haier.uhome.uplus|-1314|null|10471|0: Notification(pri=0 contentView=null vibrate=null sound=null defaults=0x0 flags=0x62 color=0xff607d8b vis=PRIVATE)) TestRes=null

那很有可能在自定义的布局R.layout.content_view中有不支持的布局或控件。官方自定义通知栏RemoteViews只支持以下布局和控件:
布局容器Layout:
FrameLayout,LinearLayout,RelativeLayout,GridLayout
控件Component:
AnalogClock,Button,Chronometer,ImageButton,ImageView,ProgressBar,
TextView,ViewFlipper,ListView,GridView,StackView,AdapterViewFlipper

修改布局文件,使其符合上述的条件即可。
 

猜你喜欢

转载自blog.csdn.net/chenzhengfeng/article/details/108729750