Android widget background not appearing, not responding
The background image of a widget I've created in an application has suddenly stopped appearing. Additionally, the widget no longer appears to respond to click events when I run the application in an emulator. Here are the Android project files:
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.java
and res/drawable/widget_background.png
both exist. I am able to open the latter in a standard image viewer. When executed, the widget appears as in this screenshot: http://i.stack.imgur.com/b43Ya.png. The logcat output is rather long to include here, but this is one line I noticed that appears relevant.
08-22 19:04:46.182: WARN/AppWidgetHostView(100): can't inflate defaultView because mInfo is missing
This error does appear in the Android source for android.appwidget.AppWidgetHostView
, but I'm not certain how to correct the issue and Google is providing little useful information about the error.
Has anyone else experienced this or found a fix? I'm able to replicate this issue with a brand new project and all references to source files and resources appear consistent, so I'm wondering if the issue isn't specific to my development environment. Any an开发者_开发问答d all feedback is appreciated.
that error is logged when the widget host does not have any widget provider metadata My guess is that there is some problem with the metadata file. I notice that you have your updatePeriod set to 0. Try setting it to half an hour (1800000) and see what happens. To be safe, temporarily remove the configuration tag as well, to make it simple as possible.
At the suggestion of a colleague, I tried nuking and recreating my AVD. For some reason, this appeared to fix the problem. The widget image is now found and properly displayed.
精彩评论