我的可配置时钟小部件有一个奇怪的问题,我无法调试:(这是我的第一个小部件,它很简单;),它是一个后台可配置时钟小部件。当用户选择小部件时,将启动配置活动。在此活动中,用户可以选择时钟的小部件背景。当用户完成配置时,带有用户背景的“标准时钟小部件”会出现在启动器的屏幕上,仅此而已。我对其进行了彻底的测试,它似乎可以正常工作,直到...我遇到的问题是,有时当我打开并解锁屏幕时,我的小部件“消失”了一个灰色的盒子,上面有可怕的“加载小部件问题”错误:(所以,我遇到了一个错误,我无法获得可重现的场景,也许与生命周期小部件方法错误有关?当错误出现时,我在 logcat 中看不到任何问题,所以我完全迷失了。
这是此小部件的简单代码:(GB 2.3.3)。我已经取出了不重要的部分。
1.- 小部件提供者 xml
<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" >
</appwidget-provider>
2.- AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="........"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="10"
android:targetSdkVersion="10" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="........"
android:label="@string/app_name"
android:screenOrientation="portrait" >
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_CONFIGURE" />
</intent-filter>
</activity>
<receiver android:name="........." >
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/........" />
</receiver>
</application>
</manifest>
3.- 小部件布局我有几个小部件布局。用户从配置活动中选择一个所有布局几乎相同,只有微小的变化(ImageView drawable):
<?xml version="1.0" encoding="utf-8"?>
<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" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:scaleType="fitXY"
android:src="@drawable/...." />
<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/...." />
</FrameLayout>
4.- 小部件提供程序(Java)
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);
}
}
}
5.- 配置活动(Java)
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();
}
}