【问题标题】:Couldn't expand RemoteViews无法展开 RemoteViews
【发布时间】:2013-09-04 16:02:24
【问题描述】:

我尝试创建自定义通知,但收到以下异常:

FATAL EXCEPTION: main
android.app.RemoteServiceException: Bad notification posted from package com.my.app: Couldn't expand RemoteViews for: StatusBarNotification(pkg=com.my.app user=UserHandle{0} id=1 tag=null score=0: Notification(pri=0 contentView=com.my.app/0x7f03001b vibrate=null sound=null defaults=0x0 flags=0x2 kind=[null]))
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1423)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:137)
    at android.app.ActivityThread.main(ActivityThread.java:5103)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:525)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
    at dalvik.system.NativeStart.main(Native Method)

我的代码如下所示:

Intent intent = new Intent(this, MyFragmentActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent launchIntent = PendingIntent.getActivity(this, 0, intent, 0);

RemoteViews notificationView = new RemoteViews(this.getPackageName(), R.layout.notification_layout);
notificationView.setImageViewBitmap(R.id.notification_icon, icon);
notificationView.setTextViewText(R.id.present_text, "Text Test");

Notification.Builder builder = new Notification.Builder(getApplicationContext());
builder
    .setContentTitle("Titel Test")
    .setSmallIcon(R.drawable.ic_launcher_icon)
    .setContentIntent(launchIntent)
    .setOngoing(true)
    .setWhen(0)
    .setTicker("ticker Test")
    .setContent(notificationView);

NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(NOTIFICATION_ID, builder.getNotification());

布局如下:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <ImageView
        android:id="@+id/notification_icon"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_alignParentLeft="true" />

    <EditText
        android:id="@+id/present_text"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/notification_icon"
        android:gravity="center_vertical" />

</RelativeLayout>

我正在运行安卓 4.3。我之前尝试过 NotificationCompat 并得到同样的错误。

有人有想法吗?

【问题讨论】:

  • 请发帖res/layout/notification_layout.xml
  • 我已经添加了布局。
  • 别忘了不要把vectorDrawables放到Notification,同样的例外。

标签: android exception android-notifications


【解决方案1】:

并非所有视图都可以在 RemoteViews 中使用。 RemoteViews 对象可以支持以下布局类:

FrameLayout

LinearLayout

RelativeLayout

GridLayout

AnalogClock

Button

Chronometer

ImageButton

ImageView

ProgressBar

TextView

ViewFlipper

ListView

GridView

StackView

AdapterViewFlipper

您不能使用其他布局类,例如 EditTextCustomViews

【讨论】:

  • 如果我们使用自己的进度条,如何以编程方式更改进度条
【解决方案2】:

如果您的 RemoteView 中有一个 ScrollView,请将其删除。这对我有用。

【讨论】:

    【解决方案3】:

    我发现使用 android:background="?attr/colorPrimaryDark" 也会导致崩溃。 当更改为 android:background="@color/colorPrimaryDark" 它停止崩溃。

    【讨论】:

      【解决方案4】:

      对我来说,这是因为我错误地设置了 remoteViews TextViews 中的文本。我正在这样做:

      remoteViews.setString(R.id.txt, "setText", "text");
      

      使用其中一种方法为我解决了这个问题:

      remoteViews.setCharSequence(R.id.txt, "setText", "text");
      //Or simply:
      remoteViews.setTextViewText(R.id.txt, "text");
      

      【讨论】:

      • 使用 setCharSequence 而不是 setTextViewText 对我有用。
      【解决方案5】:

      您不能将EditText 放入RemoteViewsRemoteViews 仅限于少数可能的小部件,特别是 those documented for use on an app widget。而且,对于Notification,我怀疑AdapterView 选项(如ListView)是否也能正常工作。

      【讨论】:

      • @CommonsWare 它也不允许我在 textview 上使用自定义样式是他们的任何解决方案吗?
      • @NoumanCh:我不记得尝试过。如果您无法更直接地找到有关此主题的现有问题,请提出单独的 Stack Overflow 问题。提供minimal reproducible example,展示您如何尝试使用自定义样式,并更详细地说明您的结果是什么。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-13
      • 1970-01-01
      • 2017-07-06
      • 1970-01-01
      相关资源
      最近更新 更多