Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid Widget - Weird "loading Widget" error
    text
    copied!<p>I am having a weird problem with my configurable clock widget I´m not being able to debug :( This is my first Widget an it´s an easy one ;), It´s a background configurable clock widget. When the user selects the widget the configuration activity is launched. In this activity the user may choose the clock´s widget background. When the user finishes the configuration a "standard clock widget" with the user´s background appears on the launcher´s screen and that´s all. I tested it thoroughly and it seems to works right till... The problem I´ve got it´s that from time to time when I turn on and unlock the screen, my widget it´s "gone" and I´ve got a grey box with the dreaded "Problem loading Widget" error on it :( So, I´ve got an error that I can´t get a reproducible scenario for it, maybe a life-cycle widget method error related? When the error appears I see no problem in the logcat so I´m completely lost.</p> <p>This is the easy code for this widget: (GB 2.3.3). I´ve taken out not important bits.</p> <p>1.- Widget Provider xml</p> <pre><code>&lt;appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android" android:minHeight="190dp" android:minWidth="170dp" android:configure="......" android:initialLayout="@drawable/......" android:updatePeriodMillis="30000" &gt; &lt;/appwidget-provider&gt; </code></pre> <p>2.- AndroidManifest.xml</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;manifest xmlns:android="http://schemas.android.com/apk/res/android" package="........" android:versionCode="1" android:versionName="1.0" &gt; &lt;uses-sdk android:minSdkVersion="10" android:targetSdkVersion="10" /&gt; &lt;application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" &gt; &lt;activity android:name="........" android:label="@string/app_name" android:screenOrientation="portrait" &gt; &lt;intent-filter&gt; &lt;action android:name="android.appwidget.action.APPWIDGET_CONFIGURE" /&gt; &lt;/intent-filter&gt; &lt;/activity&gt; &lt;receiver android:name="........." &gt; &lt;intent-filter&gt; &lt;action android:name="android.appwidget.action.APPWIDGET_UPDATE" /&gt; &lt;/intent-filter&gt; &lt;meta-data android:name="android.appwidget.provider" android:resource="@xml/........" /&gt; &lt;/receiver&gt; &lt;/application&gt; &lt;/manifest&gt; </code></pre> <p>3.- Widget Layout I´ve got several widget layouts.The user selects one from the configuration activity All the layouts are almost identical with minor changes (ImageView drawable):</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/LinearLayout01" android:layout_width="160dp" android:layout_height="200dp" android:orientation="vertical" &gt; &lt;ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:adjustViewBounds="true" android:scaleType="fitXY" android:src="@drawable/...." /&gt; &lt;AnalogClock android:layout_width="wrap_content" android:layout_height="138dp" android:layout_gravity="bottom" android:dial="@drawable/...." android:hand_hour="@drawable/...." android:hand_minute="@drawable/...." /&gt; &lt;/FrameLayout&gt; </code></pre> <p>4.- Widget Provider (Java)</p> <pre><code>public class ClockProvider extends AppWidgetProvider { public static int anoEscudo; @Override public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { super.onUpdate(context, appWidgetManager, appWidgetIds); RemoteViews remoteView = null; for (int appWidgetId : appWidgetIds) { // Actualizamos la variable para luego poder modificar el fondo switch (anoEscudo) { case (...): remoteView = new RemoteViews(context.getPackageName(), R.layout....); break; ... } appWidgetManager.updateAppWidget(appWidgetId, remoteView); } } } </code></pre> <p>5.- Configuration Activity (Java)</p> <pre><code>package ...; public class ConfigurationActivity extends Activity { private int appWidgetId; ImageView escudo; TextView descripcion; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // get the appWidgetId of the appWidget being configured Intent launchIntent = getIntent(); Bundle extras = launchIntent.getExtras(); appWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID); // set the result for cancel first // if the user cancels, then the appWidget // should not appear Intent cancelResultValue = new Intent(); cancelResultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId); setResult(RESULT_CANCELED, cancelResultValue); // show the user interface of configuration setContentView(R.layout.activity_configuration); // Setting configuration Activity graphical elements // .. } //Changing the Widget configuration private void ... (View view) { switch (escudoSeleccionado) { case 1: ClockProvider.anoEscudo = ...; break; ... } } // Finish the configuration Activity public void ... (View view) { Intent resultValue = new Intent(); resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId); setResult(RESULT_OK, resultValue); new ClockProvider().onUpdate(this, AppWidgetManager.getInstance(this), new int[] { appWidgetId }); finish(); } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload