我在應用程序中創建的小部件的背景圖像突然停止出現。此外,當我在模擬器中運行應用程序時,該窗口小部件不再顯示爲響應點擊事件。下面是Android項目文件: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 /佈局/ 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
和res/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