2011-08-23 46 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