学习使用RemoteViews时,给ImageView设置图片时报错:
Caused by: android.widget.RemoteViews$ActionException: android.widget.RemoteViews$ActionException: view: androidx.appcompat.widget.AppCompatImageView can't use method with RemoteViews: setImageResource(int)
at android.widget.RemoteViews$ReflectionAction.apply(RemoteViews.java:1459)
at android.widget.RemoteViews.performApply(RemoteViews.java:3563)
at android.widget.RemoteViews.reapply(RemoteViews.java:3517)
at android.widget.RemoteViews.reapply(RemoteViews.java:3500)
。。。。。。
Activity.access$updateUI(ShowNotificationWithCustomRemoteViewsActivity.kt:19)
at com.wx.study.chapter5.activity.customremoteviews.ShowNotificationWithCustomRemoteViewsActivity$mRemoteViewsReceiver$1.onReceive(ShowNotificationWithCustomRemoteViewsActivity.kt:49)
at android.app.LoadedApk$ReceiverDispatcher$Args.lambda$getRunnable$0(LoadedApk.java:1391)
at android.app.-$$Lambda$LoadedApk$ReceiverDispatcher$Args$_BumDX2UKsnxLVrE6UJsJZkotuA.run(Unknown Source:2)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
Caused by: android.widget.RemoteViews$ActionException: view: androidx.appcompat.widget.AppCompatImageView can't use method with RemoteViews: setImageResource(int)
at android.widget.RemoteViews.getMethod(RemoteViews.java:1004)
at android.widget.RemoteViews.access$400(RemoteViews.java:121)
at android.widget.RemoteViews$ReflectionAction.apply(RemoteViews.java:1457)
at android.widget.RemoteViews.performApply(RemoteViews.java:3563)
at android.widget.RemoteViews.reapply(RemoteViews.java:3517)
at android.widget.RemoteViews.reapply(RemoteViews.java:3500)
at com.wx.study.chapter5.activity.customremoteviews.ShowNotificationWithCustomRemoteViewsActivity.updateUI(ShowNotificationWithCustomRemoteViewsActivity.kt:61)
at com.wx.study.chapter5.activity.customremoteviews.ShowNotificationWithCustomRemoteViewsActivity.access$updateUI(ShowNotificationWithCustomRemoteViewsActivity.kt:19)
at com.wx.study.chapter5.activity.customremoteviews.ShowNotificationWithCustomRemoteViewsActivity$mRemoteViewsReceiver$1.onReceive(ShowNotificationWithCustomRemoteViewsActivity.kt:49)
at android.app.LoadedApk$ReceiverDispatcher$Args.lambda$getRunnable$0(LoadedApk.java:1391)
at android.app.-$$Lambda$LoadedApk$ReceiverDispatcher$Args$_BumDX2UKsnxLVrE6UJsJZkotuA.run(Unknown Source:2)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
跟踪RemoteViews源码,从android.widget.RemoteViews#setImageViewResource()开始一路跟踪到报错的位置,在android.widget.RemoteViews#getMethod方法中有这样一段代码
if (!method.isAnnotationPresent(RemotableViewMethod.class)) {
throw new ActionException("view: " + klass.getName()
+ " can't use method with RemoteViews: "
+ methodName + getParameters(paramType));
}
这段代码的意思是没有RemotableViewMethod注解的View是不能被RemoteViews设置属性值的。
查看androidx.appcompat.widget.AppCompatImageView类中对应的setImageViewResource()方法果然是没有RemotableViewMethod注解,虽然它的父类android.widget.ImageView对应的方法是有注解的,但是由于RemotableViewMethod注解在声明时没有设置可以继承,所以在子类中是没有注解的
所以将AppCompatImageView改为ImageView即可,需要注意的是当Activity使用的是AppCompatActivity时也要改为Activity。在AppCompatActivity中布局中使用的ImageView和TextView等也会在加载布局时被转换成对应的兼容类,兼容类都是这些类的子类,有些方法被继承后没有加上RemotableViewMethod注解,不能被RemoteViews使用