【问题标题】:Couldn't expand RemoteViews for: StatusBarNotification无法展开 RemoteViews:StatusBarNotification
【发布时间】:2015-01-11 18:07:16
【问题描述】:

之前有人问过这个问题,但没有一个答案对我有帮助,因此发布了我的案例。

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

android.app.RemoteServiceException: 错误通知从 com.eswaraj.app.eswaraj 包:无法扩展 RemoteViews: StatusBarNotification(pkg=com.eswaraj.app.eswaraj user=UserHandle{0} id=8888 tag=null score=0: Notification(pri=0 contentView=com.eswaraj.app.eswaraj/0x7f030052 振动=null 声音=null 默认值=0x0 标志=0x0 种类=[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());
    }

任何帮助将不胜感激。

【问题讨论】:

    标签: android android-layout notifications remoteview


    【解决方案1】:

    简而言之,

    com.makeramen.RoundedImageView

    打扰了你的工作。

    并非所有视图都可以在用于构建自定义通知的 RemoteView 中使用。根据this official page,只有以下内容可用于创建 RemoteViews:

    • 框架布局
    • 线性布局
    • 相对布局
    • 网格布局
    • 模拟时钟
    • 按钮
    • 天文钟
    • 图像按钮
    • 图像视图
    • 进度条
    • 文本视图
    • ViewFlipper
    • 列表视图
    • 网格视图
    • 堆栈视图
    • AdapterViewFlipper

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

    【讨论】:

    • 就我而言,我使用的是AppCompatImageView。切换到ImageView 解决了这个问题。
    【解决方案2】:

    我也遇到了同样的问题,我使用的是自定义的 TextView,求助于 Stock TextView 解决了我的问题。

    【讨论】:

      【解决方案3】:

      我也有同样的问题。根据接受的答案,可以在通知中使用特定的控件。我的布局中有一个用于通知的视图,如下所示。

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

      这是导致崩溃的原因。我删除了它,它工作正常。希望它可以帮助某人。

      【讨论】:

        【解决方案4】:

        我在使用约束布局时遇到了同样的问题,然后我只是按照文档https://developer.android.com/guide/topics/appwidgets/index.html#CreatingLayout 并更改为线性布局并且它起作用了。

        【讨论】:

        • 请从链接中添加一些附加信息,因为链接本身可能会在将来断开
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-04-03
        • 2018-06-15
        • 2017-07-06
        • 2013-09-04
        • 1970-01-01
        相关资源
        最近更新 更多