更改Widget的图标的onupdate图标、Widget、onupdate

2023-09-08 09:51:09 作者:罗涛……

我有意味着每收到更新播出时间来改变其图标的Widget。然而,窗口小部件从来没有设法正确显示其图标,显示文本问题加载插件。该logcat的消息是:

  WARN / AppWidgetHostView(612):updateAppWidget找不到任何观点,使用错误观点
WARN / AppWidgetHostView(612):android.widget.RemoteViews $ ActionException:找不到观点:0x7f060003
 

在$ C $下我的onupdate是:

 公共类ImageWidgetProvider扩展AppWidgetProvider {
私有静态最后字符串变量=史蒂夫;

公共静态最终诠释[] IMAGES = {R.drawable.ic_launcher_alarmclock,
    /*还有很多*/};

@覆盖
公共无效的OnUpdate(上下文的背景下,AppWidgetManager appWidgetManager,INT [] appWidgetIds){

    对于(INT appWidgetId:appWidgetIds){
        Log.d(TAG的onupdate:);
        INT imageNum =(新java.util.Random中的()nextInt(IMAGES.length)。);
        Log.d(TAG,Integer.toString(图片[imageNum]));
        RemoteViews远程视窗=新RemoteViews(context.getPackageName(),R.layout.widget);
        remoteView.setImageViewResource(R.id.image_in_widget,图片[imageNum]);
        appWidgetManager.updateAppWidget(appWidgetId,远程视窗)
    }

}
}
 

现在,当我在R.id.image_in_widget它带来了它的值等于0x7f060003鼠标悬停 - 认为它无法找到根据logcat的。使用第二个日志声明,我验证了图像[imageNum]确实指向一个随机图像,从图像阵列。 (如果它的事项,它出来作为一个十进制值,而不是一个十六进制的。)任何想法,我做错了什么?非常感谢!

-

编辑:这是小部件,其中image_in_widget ImageView的声明布局文件

 < XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT>

< ImageView的机器人:名称=@ + ID / image_in_widget
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT/>
< / LinearLayout中>
 
Android桌面小部件AppWidget 1

解决方案

这篇文章解决了我的问题

I have a widget that is meant to change its icon every time it receives the Update broadcast. However, the widget never manages to display its icon properly, displaying the text "Problem loading widget". The Logcat message is:

WARN/AppWidgetHostView(612): updateAppWidget couldn't find any view, using error view
WARN/AppWidgetHostView(612): android.widget.RemoteViews$ActionException: can't find view: 0x7f060003

The code for my onUpdate is:

public class ImageWidgetProvider extends AppWidgetProvider{
private  static final String TAG = "Steve";

public static final int[] IMAGES = { R.drawable.ic_launcher_alarmclock,
    /*and many more*/};

@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds){

    for (int appWidgetId : appWidgetIds) {
        Log.d(TAG, "onUpdate:");
        int imageNum = (new java.util.Random().nextInt(IMAGES.length));
        Log.d(TAG, Integer.toString(IMAGES[imageNum]));
        RemoteViews remoteView = new RemoteViews(context.getPackageName(), R.layout.widget);
        remoteView.setImageViewResource(R.id.image_in_widget, IMAGES[imageNum]);
        appWidgetManager.updateAppWidget(appWidgetId, remoteView);
    }

}
}

Now when I hover the mouse over "R.id.image_in_widget" it brings up that its value is equal to 0x7f060003 - the view that it can't find according to Logcat. Using the second Log statement, I verified that IMAGES[imageNum] does indeed refer to a random image from the IMAGES array. (In case it matters, it comes out as a decimal value rather than a hexadecimal one.) Any ideas what I'm doing wrong? Thanks a lot!

--

Edit: Here is the layout file for the widget, where the image_in_widget ImageView is declared.

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

<ImageView android:name="@+id/image_in_widget"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>
</LinearLayout>

解决方案

this article solved my problem