无法展开StatusBarNotification的RemoteViews。

8

这个问题以前已经被问过,但是没有一个答案能帮到我,所以我来发表我的情况。

我正在尝试使用布局文件构建自定义通知。但是我得到了以下错误:

android.app.RemoteServiceException: Bad notification posted from package com.eswaraj.app.eswaraj: Couldn't expand RemoteViews for: StatusBarNotification(pkg=com.eswaraj.app.eswaraj user=UserHandle{0} id=8888 tag=null score=0: Notification(pri=0 contentView=com.eswaraj.app.eswaraj/0x7f030052 vibrate=null sound=null defaults=0x0 flags=0x0 kind=[null]))

我的布局文件如下:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="left">

<com.makeramen.RoundedImageView
    android:id="@+id/nImage"
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:src="@drawable/profile_image"
    android:padding="15dp"
    android:scaleType="fitCenter"
    app:riv_corner_radius="30dip"
    app:riv_border_width="2dip"
    app:riv_border_color="#333333"
    app:riv_mutate_background="true"
    app:riv_oval="true"/>

<TextView
    android:id="@+id/nTitle"
    android:text="eSwaraj: Your voice is heard"
    android:layout_toRightOf="@+id/nImage"
    android:paddingTop="12dp"
    android:textSize="20dp"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

<TextView
    android:id="@+id/nMessage"
    android:layout_below="@+id/nTitle"
    android:layout_toRightOf="@+id/nImage"
    android:text="Your comaplaint viewed"
    android:textSize="15dp"
    android:paddingTop="5dp"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

<TextView
    android:id="@+id/nTime"
    android:layout_toRightOf="@+id/nImage"
    android:layout_below="@+id/nMessage"
    android:textSize="13dp"
    android:paddingTop="5dp"
    android:text="12:23PM"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

</RelativeLayout>

我正在发送以下通知:

public void sendNotification(Context caller, Class<?> activityToLaunch, Bitmap icon,   GcmMessageDto gcmMessageDto, int id) {
        Intent toLaunch;
        RemoteViews remoteViews = new RemoteViews(caller.getPackageName(), R.layout.notification);
        if(activityToLaunch != null) {
            toLaunch = new Intent(caller, activityToLaunch);
       }
       else {
            toLaunch = new Intent();
        }
        PendingIntent intentBack = PendingIntent.getActivity(caller, 0, toLaunch, 0);

        NotificationManager notifier = (NotificationManager) caller.getSystemService(Context.NOTIFICATION_SERVICE);
        NotificationCompat.Builder mBuilder =
                new NotificationCompat.Builder(caller)
                    .setSmallIcon(android.R.drawable.ic_notification_overlay)
                    .setContentIntent(intentBack);

        remoteViews.setTextViewText(R.id.nMessage, gcmMessageDto.getMessage());
        remoteViews.setTextViewText(R.id.nTime, new Date().toString());
        if(icon != null) {
            remoteViews.setImageViewBitmap(R.id.nImage, icon);
        }
        mBuilder.setContent(remoteViews);

        notifier.notify(id, mBuilder.build());
    }

非常感谢您的帮助。

4个回答

20

简单来说,

com.makeramen.RoundedImageView

影响了您的工作。

并非所有视图都可以在用于构建自定义通知的 RemoteViews 中使用。根据此官方页面,只有以下内容可用于创建 RemoteViews:

  • FrameLayout
  • LinearLayout
  • RelativeLayout
  • GridLayout
  • AnalogClock
  • Button
  • Chronometer
  • ImageButton
  • ImageView
  • ProgressBar
  • TextView
  • ViewFlipper
  • ListView
  • GridView
  • StackView
  • AdapterViewFlipper

因此,您必须为您修改的 ImageView 类找到解决方法,并改用普通的 ImageView。


在我的情况下,我使用的是 AppCompatImageView。切换到 ImageView 解决了这个问题。 - iTurki

0

我曾经遇到过同样的问题。根据被接受的答案,有特定的控件可以用于通知。我在我的布局中为通知设置了一个视图,如下所示。

<View
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="0.04"/>

这是导致崩溃的原因。我把它移除了,然后它就正常工作了。希望能对某些人有所帮助。


0

我也遇到了同样的问题,我使用了自定义的TextView,改用原生的TextView解决了我的问题。


0

请从链接中添加一些额外的信息,因为链接本身可能会在未来失效。 - Asad-ullah Khan

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接