无法展开RemoteViews [英] Couldn't expand RemoteViews

查看:143
本文介绍了无法展开RemoteViews的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I try to create a custom notification but getting the following exception:

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)

我的代码看起来像这样:

My code looks like this:

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>

我正在运行android 4.3。我已经尝试过NotificationCompat并得到相同的错误。

I'm running android 4.3. I've tried before with NotificationCompat and get the same error.

有人有想法吗?

推荐答案

您不能将 EditText 放入 RemoteViews 中。 RemoteViews 仅限于少数可能的小部件,特别是在应用程序小部件上使用的文档。而且,对于通知,我怀疑 AdapterView 选项,如 ListView ,将会工作。

You cannot put an EditText into a RemoteViews. RemoteViews is limited to a handful of possible widgets, specifically those documented for use on an app widget. And, for a Notification, I doubt that the AdapterView options, like ListView, will work either.

这篇关于无法展开RemoteViews的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆