【问题标题】:Android Widget - Weird "loading Widget" errorAndroid Widget - 奇怪的“加载 Widget”错误
【发布时间】:2013-11-07 19:37:26
【问题描述】:

我的可配置时钟小部件有一个奇怪的问题,我无法调试 :( 这是我的第一个小部件,它很简单;),它是一个后台可配置的时钟小部件。 当用户选择小部件时,将启动配置活动。在此活动中,用户可以选择时钟的小部件背景。当用户完成配置时,带有用户背景的“标准时钟小部件”会出现在启动器的屏幕上,仅此而已。 我对它进行了彻底的测试,它似乎可以正常工作,直到...... 我遇到的问题是,当我打开并解锁屏幕时,我的小部件“不见了”,而且我有一个灰色框,上面有可怕的“加载小部件问题”错误:( 所以,我有一个错误,我无法获得可重现的场景,也许是与生命周期小部件方法错误相关? 当错误出现时,我在 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();
}   
}

【问题讨论】:

  • 你调试过这个应用程序吗?
  • 您尝试了哪些方法来弄清楚发生了什么?你从这些方法中得到了什么结果?我们不能在不知道您已经尝试过的情况下提出建议,否则我们只会重蹈覆辙。
  • 我不知道如何将调试器与小部件一起使用。我在我的手机上对其进行了测试,它一直在工作,直到某种生命周期事件使它显示“问题小部件加载文本”错误。在让移动设备(和小部件)运行几个小时并打开屏幕后会发生该错误。

标签: android widget


【解决方案1】:
  • 可能与updatePeriodMillis有关,请查看doc. for updatePeriodMillis

updatePeriodMillis 属性定义了 App Widget 框架通过调用 onUpdate() 回调方法向 AppWidgetProvider 请求更新的频率。使用此值不能保证实际更新准确及时,我们建议尽可能不频繁地更新 - 可能不超过每小时一次以节省电池电量。您还可以允许用户在配置中调整频率——有些人可能希望股票代码每 15 分钟更新一次,或者每天只更新四次。

如果不是这样,我建议查看以下参考资料

  • 具有配置活动的 Android 应用小部件
    link 1 , link 2
  • Tutorial 创建一个类似的应用程序。

【讨论】:

    猜你喜欢
    • 2017-06-02
    • 1970-01-01
    • 2011-11-18
    • 1970-01-01
    • 2023-03-04
    • 1970-01-01
    • 1970-01-01
    • 2018-01-23
    • 2011-03-16
    相关资源
    最近更新 更多