Android 不能';t扩展远程视图
我尝试创建自定义通知,但出现以下异常:Android 不能';t扩展远程视图,android,exception,android-notifications,Android,Exception,Android Notifications,我尝试创建自定义通知,但出现以下异常: 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: Notificatio
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,但也遇到了同样的错误
有人有主意吗?您不能将
编辑文本
放入远程视图
<代码>远程视图仅限于少数可能的小部件,特别是。而且,对于通知
,我怀疑AdapterView
选项,如列表视图
,是否也能正常工作。对于我来说,这是因为我在RemoteView文本视图中设置的文本不正确。我是这样做的:
remoteViews.setString(R.id.txt, "setText", "text");
使用以下方法之一为我解决了这个问题:
remoteViews.setCharSequence(R.id.txt, "setText", "text");
//Or simply:
remoteViews.setTextViewText(R.id.txt, "text");
并非所有视图都可以在RemoteView中使用。RemoteView对象可以支持以下布局类: <代码>框架布局
LinearLayout
RelativeLayout
GridLayout
模拟时钟
按钮
计时器
ImageButton
ImageView
ProgressBar
TextView
ViewFlipper
ListView
GridView
StackView
adapterViewLipper
您不能使用其他布局类,如
EditText
或customview我发现使用android:background=“?attr/colorPrimaryDark”也会导致崩溃。
当更改为android:background=“@color/colorPrimaryDark”时,它停止崩溃。如果远程视图中有滚动视图,请将其删除。这对我很有用。请发布
res/layout/notification\u layout.xml
。我已经添加了布局。别忘了不要将矢量绘图功能放到通知中,这也是一个例外。使用setCharSequence
而不是setTextViewText
对我来说很有效。@Commonware也不允许我在textview上使用自定义样式,他们有什么解决办法吗?@NoumanCh:我不记得试过那样做。如果您无法更直接地找到有关此主题的现有问题,请提出单独的堆栈溢出问题。提供一个,显示您如何尝试使用自定义样式,并更详细地解释您的结果。如果我们使用自己的ProgressBar,如何以编程方式更改进度栏