2011-08-23 45 views
4

我在應用程序中創建的小部件的背景圖像突然停止出現。此外,當我在模擬器中運行應用程序時,該窗口小部件不再顯示爲響應點擊事件。下面是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.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,但我不能確定如何糾正這一問題,谷歌將提供有關錯誤的一點有用的信息。

有沒有其他人經歷過這個或找到了解決辦法?我可以用一個全新的項目來複制這個問題,所有對源文件和資源的引用看起來都是一致的,所以我想知道這個問題是不是特定於我的開發環境。任何和所有的反饋意見。

+0

已投票。希望有人回答你。 – joedevon

回答

0

在一位同事的建議下,我嘗試了nucking並重新創建了我的AVD。出於某種原因,這似乎解決了這個問題。小部件圖像現在可以找到並正確顯示。

0

當小部件主機沒有任何小部件提供程序元數據時記錄錯誤我的猜測是元數據文件存在一些問題。我注意到你的updatePeriod設置爲0.嘗試將它設置爲半小時(1800000),看看會發生什麼。爲了安全起見,暫時移除配置標籤,以儘可能簡化。

+0

可悲的是,沒有效果。 :( – elazar