Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
146 views
in Technique[技术] by (71.8m points)

Why is android trying to read an xml layout file from the /res/layout when I have created the layout in code?

This question is in respect to RemoteViews. It works as it should in normal activities, but not in widgets:

I have that in my ids.xml:

    <item type="layout" name="linear_layout_for_widget"></item>

I have this in my WidgetProvider class:

    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);

Where as Layout4x1Provider is a helper class, which constructs my layout tree in code. If I do the equivalent of this in my configuration activity (to display a preview) it will display correctly:

    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);

As a RemoteView, it does not work althogh the docs state that the addView() method of the RemoteView class is equivalent to the addView() method of the ViewGroup class.

Code compiles, only an exception is thrown in the 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

So, obviously android is looking for the a corresponding xml file...So, how do I tell android not to look for the xml file. I assumed that the declaration of the layout resource in ids.xml should preent android from looking for a physical file. in the code which creates the layout, I have:

    linearLayoutForWidget.setId(R.layout.linear_layout_for_widget);

Why does this work as activity but not in a widget/RemoteView? Instead of displaying the widget, "Problem loading the widget" is displayed. On a side note: I already changed the type of the resource item from "layout" to "id" and resp. in code, to no avail.

It really bugs me, because I have seen many widgets which are configurable on-the-fly and to such an extent that I doubt they are based on huge collections of resource files. For example, widgets which allow the user to choose any baclgroundcolor with a color picker.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Because you told it to:

RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget4x1_layout);

That said, I think it pretty much always does.

Modifying/adding Views from code is not mutually exclusive from using an XML layout though. Just put a minimal amount in your XML layout, maybe just a single LinearLayout. Then you can add to it as desired with addView.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...