Change widget icon from onUpdate

I have a widget that is designed to change its icon every time it receives broadcast updates. However, the widget can never correctly display its icon, displaying the text "Widget loading problem." Logcat message:

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

Code for my onUpdate:

 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 over R.id.image_in_widget, it discovers that its value is 0x7f060003 - a view that it cannot find according to Logcat. Using the second Log statement, I confirmed that IMAGES [imageNum] really refers to a random image from the IMAGES array. (In case it matters, it appears as a decimal value, not a hexadecimal value.) Any ideas what I'm doing wrong? Many thanks!

-

Edit: Here is the layout file for the widget where 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> 
+2
source share
4 answers

Another reason this could happen (for those who search the Internet for this issue, is more than an original poster):

If you have separate layouts for landscape and portrait, two layouts should contain any views that you specify during the update. If you refer to a view that is present in only one mode, say, a portrait, and then in the landscape you will get the message "Problem with loading widget" in the widget window.

A simple solution is to set the visibility to β€œGONE” in a mode in which you do not want these views to be displayed.

+2
source

Android 1.5 has a problem not causing OnDelete for widgets.

Put this code in your AppWidgetProvider and it should fix the problem

 public void onReceive(Context context, Intent intent) { // v1.5 fix that doesn't call onDelete Action final String action = intent.getAction(); if (AppWidgetManager.ACTION_APPWIDGET_DELETED.equals(action)) { final int appWidgetId = intent.getExtras().getInt( AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID); if (appWidgetId != AppWidgetManager.INVALID_APPWIDGET_ID) { this.onDeleted(context, new int[] { appWidgetId }); } } else { super.onReceive(context, intent); } } 
+1
source

This may not apply in your example, but I also had similar problems with the application widgets. Android 1.5 has an annoying problem with widgets on the main screen. In particular, widget applications are still active in memory, but no longer visible.
You list through appWidgetIds, check if this array contains exactly the number of expected identifiers. Just add another Log.d () to register the identifiers. Perhaps you also have some ghost widgets.
What version for Android are you using?

0
source

Source: https://habr.com/ru/post/1316272/


All Articles