这个问题是关于 RemoteViews 的。它在正常活动中可以正常工作,但在小部件中却不行:
我的 ids.xml 中有这个:
<item type="layout" name="linear_layout_for_widget"></item>
我的 WidgetProvider 类中有这个:
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget4x1_layout);
LinearLayout linearLayoutForWidget = layout4x1Provider.createLinearLayoutForWidget(backgroundColor,
textColor, borderColor, borderWidth, false);
RemoteViews nestedView = new RemoteViews(context.getPackageName(), R.layout.linear_layout_for_widget);
remoteViews.removeAllViews(R.id.frame_layout_root);
remoteViews.addView(R.id.frame_layout_root, nestedView);
appWidgetManager.updateAppWidget(widgetId, remoteViews);
其中 Layout4x1Provider 是一个辅助类,它在代码中构建我的布局树。如果我在我的配置活动中执行此操作(以显示预览),它将正确显示:
Layout4x1Provider layout4x1Provider = new Layout4x1Provider(this);
FrameLayout frameLayoutRoot = (FrameLayout) findViewById(R.id.frame_layout_root);
LinearLayout linearLayoutWidget = layout4x1Provider.createLinearLayoutForWidget(backgroundColor,
textColor, borderColor, borderWidth, true);
frameLayoutRoot.removeAllViews();
frameLayoutRoot.addView(linearLayoutWidget);
作为 RemoteView,尽管文档声明 RemoteView 类的 addView() 方法等同于 ViewGroup 类的 addView() 方法,但它不起作用。
代码编译,Logcat中只抛出异常:
04-06 00:42:12.843: W/AppWidgetHostView(4671): updateAppWidget couldn't find any view, using error view
04-06 00:42:12.843: W/AppWidgetHostView(4671): android.content.res.Resources$NotFoundException: File from xml type layout resource ID #0x7f060003
04-06 00:42:12.843: W/AppWidgetHostView(4671): at android.content.res.Resources.loadXmlResourceParser(Resources.java:1916)
04-06 00:42:12.843: W/AppWidgetHostView(4671): at android.content.res.Resources.loadXmlResourceParser(Resources.java:1871)
04-06 00:42:12.843: W/AppWidgetHostView(4671): at android.content.res.Resources.getLayout(Resources.java:731)
04-06 00:42:12.843: W/AppWidgetHostView(4671): at android.view.LayoutInflater.inflate(LayoutInflater.java:318)
04-06 00:42:12.843: W/AppWidgetHostView(4671): at android.widget.RemoteViews.apply(RemoteViews.java:1303)
04-06 00:42:12.843: W/AppWidgetHostView(4671): at android.widget.RemoteViews$ViewGroupAction.apply(RemoteViews.java:844)
04-06 00:42:12.843: W/AppWidgetHostView(4671): at android.widget.RemoteViews.performApply(RemoteViews.java:1328)
04-06 00:42:12.843: W/AppWidgetHostView(4671): at android.widget.RemoteViews.apply(RemoteViews.java:1305)
04-06 00:42:12.843: W/AppWidgetHostView(4671): at android.appwidget.AppWidgetHostView.updateAppWidget(AppWidgetHostView.java:218)
04-06 00:42:12.843: W/AppWidgetHostView(4671): at android.appwidget.AppWidgetHost.createView(AppWidgetHost.java:218)
04-06 00:42:12.843: W/AppWidgetHostView(4671): at org.adw.launcher.Launcher.realAddWidget(Launcher.java:4005)
04-06 00:42:12.843: W/AppWidgetHostView(4671): at org.adw.launcher.Launcher.access$25(Launcher.java:3984)
04-06 00:42:12.843: W/AppWidgetHostView(4671): at org.adw.launcher.Launcher$3.onClick(Launcher.java:1069)
04-06 00:42:12.843: W/AppWidgetHostView(4671): at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:159)
04-06 00:42:12.843: W/AppWidgetHostView(4671): at android.os.Handler.dispatchMessage(Handler.java:99)
04-06 00:42:12.843: W/AppWidgetHostView(4671): at android.os.Looper.loop(Looper.java:130)
04-06 00:42:12.843: W/AppWidgetHostView(4671): at android.app.ActivityThread.main(ActivityThread.java:3687)
04-06 00:42:12.843: W/AppWidgetHostView(4671): at java.lang.reflect.Method.invokeNative(Native Method)
04-06 00:42:12.843: W/AppWidgetHostView(4671): at java.lang.reflect.Method.invoke(Method.java:507)
04-06 00:42:12.843: W/AppWidgetHostView(4671): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842)
04-06 00:42:12.843: W/AppWidgetHostView(4671): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
04-06 00:42:12.843: W/AppWidgetHostView(4671): at dalvik.system.NativeStart.main(Native Method)
04-06 00:42:12.843: W/AppWidgetHostView(4671): Caused by: java.io.FileNotFoundException:
04-06 00:42:12.843: W/AppWidgetHostView(4671): at android.content.res.AssetManager.openXmlAssetNative(Native Method)
04-06 00:42:12.843: W/AppWidgetHostView(4671): at android.content.res.AssetManager.openXmlBlockAsset(AssetManager.java:486)
04-06 00:42:12.843: W/AppWidgetHostView(4671): at android.content.res.Resources.loadXmlResourceParser(Resources.java:1898)
04-06 00:42:12.843: W/AppWidgetHostView(4671): ... 21 more
所以,显然 android 正在寻找相应的 xml 文件......那么,我如何告诉 android 不要寻找 xml 文件。我假设 ids.xml 中布局资源的声明应该阻止 android 查找物理文件。在创建布局的代码中,我有:
linearLayoutForWidget.setId(R.layout.linear_layout_for_widget);
为什么这可以作为活动而不是在小部件/RemoteView 中工作?不显示小部件,而是显示“加载小部件时出现问题”。附带说明:我已经将资源项的类型从“布局”更改为“id”和resp。在代码中,无济于事。
这真的让我很恼火,因为我见过许多可以即时配置的小部件,以至于我怀疑它们是基于大量资源文件的。例如,允许用户使用颜色选择器选择任何背景颜色的小部件。