为什么Android的尝试读取从/ RES /布局一个xml布局文件时,我已经创建了code中的布局? [英] Why is android trying to read an xml layout file from the /res/layout when I have created the layout in code?

查看:344
本文介绍了为什么Android的尝试读取从/ RES /布局一个xml布局文件时,我已经创建了code中的布局?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题是关于RemoteViews。它的工作原理,因为它应该在正常的活动,但不是在小工具:

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

我有我的ids.xml:

I have that in my ids.xml:

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

我有这在我的WidgetProvider类:

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

在哪里为Layout4x1Provider是一个辅助类,它构建了我的布局树code。
如果我做的这相当于在我的配置活动(以显示preVIEW),它会正确显示:

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

作为一个远程视窗,这是行不通的althogh文档说明该远程视窗类的addView()方法是等效于的ViewGroup类的addView()方法。

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编译,只有一个例外,在logcat中抛出的:

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

所以,很明显Android是寻找一个相应的XML文件...所以,我怎么告诉机器人不看XML文件。我以为,布局资源的ids.xml声明应该寻找一个物理文件$ P $压抑已久的Andr​​oid。在code创造的布局,我有:

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

为什么这项工作作为活动而不是在一个小部件/远程视窗?
而不是显示部件的,将显示问题加载插件。
在一个侧面说明:我已经从布局到ID和RESP改变了资源项目的类型。在code,无济于事。

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.

这真让我心烦,因为我已经看到许多小部件这些都对即时和这样的,我怀疑他们是基于对资源文件的集合巨大的程度配置。例如,窗口小部件,允许用户选择一个颜色选择器的任何baclgroundcolor。

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.

推荐答案

由于你告诉它:

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

这是说,我认为这pretty多少总是这样。

That said, I think it pretty much always does.

修改/添加查看从code不是从虽然使用XML布局互斥秒。只要把少量在你的XML布局,也许只是一个的LinearLayout 。然后与 addView 希望您可以添加到它。

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.

这篇关于为什么Android的尝试读取从/ RES /布局一个xml布局文件时,我已经创建了code中的布局?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆