我正在使用远程视图为媒体播放器生成自定义通知。但是当我调用showNotification()方法时会出现以下异常。
android.app.RemoteServiceException: Bad notification posted from package com.sample.acv3:
Couldn't expand RemoteViews for: StatusBarNotification(pkg=com.sample.acv3 user=UserHandle{0} id=1234 tag=null score=0 key=0|com.sample.acv3|1234|null|10140: Notification(pri=0 contentView=com.sample.acv3/0x7f040055 vibrate=null sound=null defaults=0x0 flags=0x0 color=0x00000000 vis=PRIVATE))
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1526)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5461)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
我的Java代码,用于生成自定义通知。
public void showNotification() {
if (mRemoteViews == null) {
mRemoteViews = new RemoteViews(getPackageName(), R.layout.player_notification);
}
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotification = new NotificationCompat.Builder(MPService.this)
.setSmallIcon(R.drawable.status_icon)
.setOngoing(true)
.setContent(mRemoteViews);
mRemoteViews.setTextViewText(R.id.player_notification_title, AppConstant.playerTitle);
notificationManager.notify(0, mNotification.build());
}
XML代码(player_notification.xml)
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/switch_thumb_normal_material_light"
android:orientation="vertical">
<ImageView
android:id="@+id/player_image"
android:layout_width="41dp"
android:layout_height="41dp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_centerVertical="true"
android:layout_margin="16dp"
android:src="@drawable/ic_launcher_sample" />
<TextView
android:id="@+id/player_notification_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginRight="70dp"
android:layout_marginTop="10dp"
android:layout_toLeftOf="@+id/closebutton"
android:layout_toRightOf="@id/player_image"
android:singleLine="true"
android:textColor="@color/colorPrimary"
android:textSize="18sp"
/>
<TextView
android:id="@+id/textViewApp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/player_notification_title"
android:layout_marginBottom="10dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_toRightOf="@id/player_image"
android:singleLine="true"
android:text="Sample"
android:textColor="@color/colorPrimary"
/>
<ImageButton
android:id="@+id/closebutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="@dimen/list_image_margin"
android:background="@drawable/ic_clear_black_24dp" />
<ImageView
android:id="@+id/bar_btn_play"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginRight="25dp"
android:layout_toLeftOf="@id/closebutton"
android:background="@drawable/ic_play_notification"
android:backgroundTint="@color/colorPrimary"
android:visibility="gone" />
<ImageView
android:id="@+id/bar_btn_pause"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginRight="25dp"
android:layout_toLeftOf="@id/closebutton"
android:background="@drawable/ic_pause_notification"
android:backgroundTint="@color/colorPrimary" />
</RelativeLayout>
我还阅读了有关此异常的堆栈溢出帖子,但无法获得此解决方案。
我认为远程视图存在一些问题。但我无法弄明白。