首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >无法扩展RemoteViews

无法扩展RemoteViews
EN

Stack Overflow用户
提问于 2013-09-01 19:41:42
回答 4查看 21.5K关注 0票数 19

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

代码语言:javascript
复制
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)

我的代码如下所示:

代码语言:javascript
复制
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());

布局如下所示:

代码语言:javascript
复制
<?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,但也遇到了同样的错误。

有没有人有主意?

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2013-09-01 20:23:41

不能将EditText放入RemoteViews中。RemoteViews仅限于少数几个可能的小部件,特别是those documented for use on an app widget。而且,对于Notification来说,我怀疑像ListView这样的AdapterView选项也不会起作用。

票数 46
EN

Stack Overflow用户

发布于 2015-08-28 11:48:51

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

代码语言:javascript
复制
remoteViews.setString(R.id.txt, "setText", "text");

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

代码语言:javascript
复制
remoteViews.setCharSequence(R.id.txt, "setText", "text");
//Or simply:
remoteViews.setTextViewText(R.id.txt, "text");
票数 3
EN

Stack Overflow用户

发布于 2018-03-04 17:41:26

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18558066

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档