Android小部件背景未出现,无响应。

4

我创建的应用程序中一个小部件的背景图突然不再显示。此外,当我在模拟器中运行应用程序时,该小部件似乎不再响应点击事件。以下是Android项目文件:

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.widgetexample"
      android:versionCode="1"
      android:versionName="1.0">
    <uses-sdk android:minSdkVersion="-4" />
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".ConfigureActivity">
            <intent-filter>
                <action android:name="android.appwidget.action.APPWIDGET_CONFIGURE" />
            </intent-filter>
        </activity>
        <receiver android:name="WidgetProvider">
            <intent-filter>
                <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
            </intent-filter>
            <meta-data android:name="android.appwidget.provider"
                android:resource="@xml/widgetprovider" />
        </receiver>
    </application>
</manifest>

res/xml/widgetprovider.xml

<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
    android:minWidth="72dp"
    android:minHeight="72dp"
    android:updatePeriodMillis="0"
    android:initialLayout="@layout/widget"
    android:configure="com.widgetexample.ConfigureActivity">
</appwidget-provider>

res/layout/widget.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <Button
        android:id="@+id/widget_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="10dp"
        android:background="@drawable/widget_background" />
</LinearLayout>

src/com/widgetexample/WidgetProvider.java

package com.widgetexample;

import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.content.Intent;
import android.widget.RemoteViews;

public class WidgetProvider extends AppWidgetProvider {
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
        for (int widgetIndex = 0; widgetIndex < appWidgetIds.length; widgetIndex++) {
            int appWidgetId = appWidgetIds[widgetIndex];
            Intent intent = new Intent(context, ConfigureActivity.class);
            PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
            RemoteViews mRemoteViews = new RemoteViews(context.getPackageName(), R.layout.widget);
            mRemoteViews.setOnClickPendingIntent(R.id.widget_button, pendingIntent);
            appWidgetManager.updateAppWidget(appWidgetId, mRemoteViews);
        }
    }
}

src/com/widgetexample/ConfigureActivity.javares/drawable/widget_background.png 均已存在。我可以通过标准图像查看器打开后者。当执行时,小部件显示如下截图:http://i.stack.imgur.com/b43Ya.png。Logcat 输出太长了,无法在此处包含所有内容,但我注意到下面这行似乎与问题有关。

08-22 19:04:46.182: WARN/AppWidgetHostView(100): can't inflate defaultView because mInfo is missing

这个错误出现在 Android 源代码中的 android.appwidget.AppWidgetHostView,但我不确定如何修复此问题,而且谷歌对此错误提供的信息很少。

是否有其他人遇到过这个问题或找到了解决方法?我能够在全新的项目中复制这个问题,所有源文件和资源的引用看起来都是一致的,所以我想这个问题可能不是特定于我的开发环境。非常感谢任何反馈。


点赞了。希望有人能回答你。 - joedevon
2个回答

0

当小部件主机没有任何小部件提供程序元数据时,会记录该错误。 我猜测元数据文件存在问题。 我注意到您将updatePeriod设置为0。 尝试将其设置为半小时(1800000),看看会发生什么。 为了安全起见,暂时删除配置标记,使其尽可能简单。


0
在同事的建议下,我尝试了销毁并重新创建我的 AVD。出于某种原因,这似乎解决了问题。小部件图像现在被找到并正确显示。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接