无法使用自定义组件保存 Android XML 文件

发布于 2024-10-22 18:45:39 字数 7643 浏览 6 评论 0原文

我使用一些自定义组件创建了 Android XML 布局。但是当我保存 XML 文件时出现错误。由于这是我第一次使用自定义组件(小部件),我不知道为什么会收到此错误。 这是我的 xml 代码。

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:layout_marginTop="12dp"
android:orientation="vertical"
android:background="@drawable/layout_bg">

<LinearLayout
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:layout_gravity="center_horizontal"
    android:paddingLeft="12dp"
    android:paddingRight="12dp"
    android:paddingTop="30dp">

  <kankan.wheel.widget.WheelView android:id="@+id/day"
        android:layout_height="wrap_content"
        android:layout_width="46dp"/>
    <kankan.wheel.widget.WheelView android:id="@+id/month"
        android:layout_height="wrap_content"
        android:layout_width="100dp"/>
    <kankan.wheel.widget.WheelView android:id="@+id/year"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"/> 


</LinearLayout>

这就是我得到的错误。 我必须说R.java文件中没有这样的资源值

错误! NotFoundException:无法 解析资源值:0x7F020009。

android.content.res.Resources$NotFoundException: 无法解析资源值: 0x7F020009。在 com.android.layoutlib.bridge.BridgeResources.throwException(BridgeResources.java:546) 在 com.android.layoutlib.bridge.BridgeResources.getDrawable(BridgeResources.java:131) 在 kankan.wheel.widget.WheelView.initResourcesIfNecessary(WheelView.java:427) 在 kankan.wheel.widget.WheelView.calculateLayoutWidth(WheelView.java:482) 在 kankan.wheel.widget.WheelView.onMeasure(WheelView.java:518) 在 android.view.View.measure(View.java:7964) 在 android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3023) 在 android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:888) 在 android.widget.LinearLayout.measureHorizo​​ntal(LinearLayout.java:619) 在 android.widget.LinearLayout.onMeasure(LinearLayout.java:280) 在 android.view.View.measure(View.java:7964) 在 android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3023) 在 android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:888) 在 android.widget.LinearLayout.measureVertical(LinearLayout.java:350) 在 android.widget.LinearLayout.onMeasure(LinearLayout.java:278) 在 android.view.View.measure(View.java:7964) 在 android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3023) 在 android.widget.FrameLayout.onMeasure(FrameLayout.java:245) 在 android.view.View.measure(View.java:7964) 在 com.android.layoutlib.bridge.Bridge.computeLayout(Bridge.java:443) 在 com.android.ide.common.rendering.LayoutLibrary.createLegacySession(LayoutLibrary.java:404) 在 com.android.ide.common.rendering.LayoutLibrary.createSession(LayoutLibrary.java:285) 在 com.android.ide.eclipse.adt.internal.editors.layout.gle2.GraphicalEditorPart.renderWithBridge(GraphicalEditorPart.java:1506) 在 com.android.ide.eclipse.adt.internal.editors.layout.gle2.GraphicalEditorPart.renderWithBridge(GraphicalEditorPart.java:1312) 在 com.android.ide.eclipse.adt.internal.editors.layout.gle2.GraphicalEditorPart.recomputeLayout(GraphicalEditorPart.java:1043) 在 com.android.ide.eclipse.adt.internal.editors.layout.gle2.GraphicalEditorPart.onXmlModelChanged(GraphicalEditorPart.java:1005) 在 com.android.ide.eclipse.adt.internal.editors.layout.LayoutEditor.xmlModelChanged(LayoutEditor.java:303) 在 com.android.ide.eclipse.adt.internal.editors.AndroidXmlEditor$XmlModelStateListener.modelChanged(AndroidXmlEditor.java:1213) 在 org.eclipse.wst.sse.core.internal.model.AbstractStructuredModel.fireModelChanged(AbstractStructuredModel.java:553) 在 org.eclipse.wst.sse.core.internal.model.AbstractStructuredModel.internalModelChanged(AbstractStructuredModel.java:887) 在 org.eclipse.wst.sse.core.internal.model.AbstractStructuredModel.changedModel(AbstractStructuredModel.java:382) 在 org.eclipse.wst.xml.core.internal.document.DOMModelImpl.changedModel(DOMModelImpl.java:167) 在 com.android.ide.eclipse.adt.internal.editors.AndroidXmlEditor.wrapEditXmlModel(AndroidXmlEditor.java:803) 在 com.android.ide.eclipse.adt.internal.editors.uimodel.UiElementNode.setPropertyValue(UiElementNode.java:1885) 在 com.android.ide.eclipse.adt.internal.editors.layout.gle2.CanvasViewInfo.setPropertyValue(CanvasViewInfo.java:320) 在 org.eclipse.ui.views.properties.PropertySheetEntry.valueChanged(PropertySheetEntry.java:782) 在 org.eclipse.ui.views.properties.PropertySheetEntry.setValue(PropertySheetEntry.java:723) 在 org.eclipse.ui.views.properties.PropertySheetEntry.applyEditorValue(PropertySheetEntry.java:146) 在 org.eclipse.ui.views.properties.PropertySheetEntry$1.applyEditorValue(PropertySheetEntry.java:103) 在 org.eclipse.jface.viewers.CellEditor$1.run(CellEditor.java:333) 在 org.eclipse.core.runtime.SafeRunner.run(SafeRunner.java:42) 在 org.eclipse.ui.internal.JFaceUtil$1.run(JFaceUtil.java:49) 在 org.eclipse.jface.util.SafeRunnable.run(SafeRunnable.java:175) 在 org.eclipse.jface.viewers.CellEditor.fireApplyEditorValue(CellEditor.java:331) 在 com.android.ide.eclipse.adt.internal.editors.ui.EditableDialogCellEditor.handleDefaultSelection(EditableDialogCellEditor.java:264) 在 com.android.ide.eclipse.adt.internal.editors.ui.EditableDialogCellEditor$1.widgetDefaultSelected(EditableDialogCellEditor.java:83) 在 org.eclipse.swt.widgets.TypedListener.handleEvent(TypedListener.java:119) 在 org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:84) 在 org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1053) 在 org.eclipse.swt.widgets.Display.runDeferredEvents(Display.java:4066) 在 org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:3657) 在 org.eclipse.ui.internal.Workbench.runEventLoop(Workbench.java:2640) 在 org.eclipse.ui.internal.Workbench.runUI(Workbench.java:2604) 在 org.eclipse.ui.internal.Workbench.access$4(Workbench.java:2438) 在 org.eclipse.ui.internal.Workbench$7.run(Workbench.java:671) 在 org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:332) 在 org.eclipse.ui.internal.Workbench.createAndRunWorkbench(Workbench.java:664) 在 org.eclipse.ui.PlatformUI.createAndRunWorkbench(PlatformUI.java:149) 在 org.eclipse.ui.internal.ide.application.IDEApplication.start(IDEApplication.java:115) 在 org.eclipse.equinox.internal.app.EclipseAppHandle.run(EclipseAppHandle.java:196) 在 org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.runApplication(EclipseAppLauncher.java:110) 在 org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.start(EclipseAppLauncher.java:79) 在 org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:369) 在 org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:179) 在 sun.reflect.NativeMethodAccessorImpl.invoke0(Native 方法)在 sun.reflect.NativeMethodAccessorImpl.invoke(未知 来源)位于 sun.reflect.DelegatingMethodAccessorImpl.invoke(未知 来源)于 java.lang.reflect.Method.invoke(未知 来源)位于 org.eclipse.equinox.launcher.Main.invokeFramework(Main.java:619) 在 org.eclipse.equinox.launcher.Main.basicRun(Main.java:574) 在 org.eclipse.equinox.launcher.Main.run(Main.java:1407) 在 org.eclipse.equinox.launcher.Main.main(Main.java:1383)

请帮助我解决此错误。提前致谢。

I created Android XML lay out with some custom components. But when I am saving the XML file I get error. Since this is my fist time with custom components (widgets) I have no idea why am I getting this error.
This is my xml code.

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:layout_marginTop="12dp"
android:orientation="vertical"
android:background="@drawable/layout_bg">

<LinearLayout
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:layout_gravity="center_horizontal"
    android:paddingLeft="12dp"
    android:paddingRight="12dp"
    android:paddingTop="30dp">

  <kankan.wheel.widget.WheelView android:id="@+id/day"
        android:layout_height="wrap_content"
        android:layout_width="46dp"/>
    <kankan.wheel.widget.WheelView android:id="@+id/month"
        android:layout_height="wrap_content"
        android:layout_width="100dp"/>
    <kankan.wheel.widget.WheelView android:id="@+id/year"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"/> 


</LinearLayout>

And this is the error I get. I must say that there is no such a resource value in R.java file

error! NotFoundException: Could not
resolve resource value: 0x7F020009.

android.content.res.Resources$NotFoundException:
Could not resolve resource value:
0x7F020009. at
com.android.layoutlib.bridge.BridgeResources.throwException(BridgeResources.java:546)
at
com.android.layoutlib.bridge.BridgeResources.getDrawable(BridgeResources.java:131)
at
kankan.wheel.widget.WheelView.initResourcesIfNecessary(WheelView.java:427)
at
kankan.wheel.widget.WheelView.calculateLayoutWidth(WheelView.java:482)
at
kankan.wheel.widget.WheelView.onMeasure(WheelView.java:518)
at
android.view.View.measure(View.java:7964)
at
android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3023)
at
android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:888)
at
android.widget.LinearLayout.measureHorizontal(LinearLayout.java:619)
at
android.widget.LinearLayout.onMeasure(LinearLayout.java:280)
at
android.view.View.measure(View.java:7964)
at
android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3023)
at
android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:888)
at
android.widget.LinearLayout.measureVertical(LinearLayout.java:350)
at
android.widget.LinearLayout.onMeasure(LinearLayout.java:278)
at
android.view.View.measure(View.java:7964)
at
android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3023)
at
android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
at
android.view.View.measure(View.java:7964)
at
com.android.layoutlib.bridge.Bridge.computeLayout(Bridge.java:443)
at
com.android.ide.common.rendering.LayoutLibrary.createLegacySession(LayoutLibrary.java:404)
at
com.android.ide.common.rendering.LayoutLibrary.createSession(LayoutLibrary.java:285)
at
com.android.ide.eclipse.adt.internal.editors.layout.gle2.GraphicalEditorPart.renderWithBridge(GraphicalEditorPart.java:1506)
at
com.android.ide.eclipse.adt.internal.editors.layout.gle2.GraphicalEditorPart.renderWithBridge(GraphicalEditorPart.java:1312)
at
com.android.ide.eclipse.adt.internal.editors.layout.gle2.GraphicalEditorPart.recomputeLayout(GraphicalEditorPart.java:1043)
at
com.android.ide.eclipse.adt.internal.editors.layout.gle2.GraphicalEditorPart.onXmlModelChanged(GraphicalEditorPart.java:1005)
at
com.android.ide.eclipse.adt.internal.editors.layout.LayoutEditor.xmlModelChanged(LayoutEditor.java:303)
at
com.android.ide.eclipse.adt.internal.editors.AndroidXmlEditor$XmlModelStateListener.modelChanged(AndroidXmlEditor.java:1213)
at
org.eclipse.wst.sse.core.internal.model.AbstractStructuredModel.fireModelChanged(AbstractStructuredModel.java:553)
at
org.eclipse.wst.sse.core.internal.model.AbstractStructuredModel.internalModelChanged(AbstractStructuredModel.java:887)
at
org.eclipse.wst.sse.core.internal.model.AbstractStructuredModel.changedModel(AbstractStructuredModel.java:382)
at
org.eclipse.wst.xml.core.internal.document.DOMModelImpl.changedModel(DOMModelImpl.java:167)
at
com.android.ide.eclipse.adt.internal.editors.AndroidXmlEditor.wrapEditXmlModel(AndroidXmlEditor.java:803)
at
com.android.ide.eclipse.adt.internal.editors.uimodel.UiElementNode.setPropertyValue(UiElementNode.java:1885)
at
com.android.ide.eclipse.adt.internal.editors.layout.gle2.CanvasViewInfo.setPropertyValue(CanvasViewInfo.java:320)
at
org.eclipse.ui.views.properties.PropertySheetEntry.valueChanged(PropertySheetEntry.java:782)
at
org.eclipse.ui.views.properties.PropertySheetEntry.setValue(PropertySheetEntry.java:723)
at
org.eclipse.ui.views.properties.PropertySheetEntry.applyEditorValue(PropertySheetEntry.java:146)
at
org.eclipse.ui.views.properties.PropertySheetEntry$1.applyEditorValue(PropertySheetEntry.java:103)
at
org.eclipse.jface.viewers.CellEditor$1.run(CellEditor.java:333)
at
org.eclipse.core.runtime.SafeRunner.run(SafeRunner.java:42)
at
org.eclipse.ui.internal.JFaceUtil$1.run(JFaceUtil.java:49)
at
org.eclipse.jface.util.SafeRunnable.run(SafeRunnable.java:175)
at
org.eclipse.jface.viewers.CellEditor.fireApplyEditorValue(CellEditor.java:331)
at
com.android.ide.eclipse.adt.internal.editors.ui.EditableDialogCellEditor.handleDefaultSelection(EditableDialogCellEditor.java:264)
at
com.android.ide.eclipse.adt.internal.editors.ui.EditableDialogCellEditor$1.widgetDefaultSelected(EditableDialogCellEditor.java:83)
at
org.eclipse.swt.widgets.TypedListener.handleEvent(TypedListener.java:119)
at
org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:84)
at
org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1053)
at
org.eclipse.swt.widgets.Display.runDeferredEvents(Display.java:4066)
at
org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:3657)
at
org.eclipse.ui.internal.Workbench.runEventLoop(Workbench.java:2640)
at
org.eclipse.ui.internal.Workbench.runUI(Workbench.java:2604)
at
org.eclipse.ui.internal.Workbench.access$4(Workbench.java:2438)
at
org.eclipse.ui.internal.Workbench$7.run(Workbench.java:671)
at
org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:332)
at
org.eclipse.ui.internal.Workbench.createAndRunWorkbench(Workbench.java:664)
at
org.eclipse.ui.PlatformUI.createAndRunWorkbench(PlatformUI.java:149)
at
org.eclipse.ui.internal.ide.application.IDEApplication.start(IDEApplication.java:115)
at
org.eclipse.equinox.internal.app.EclipseAppHandle.run(EclipseAppHandle.java:196)
at
org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.runApplication(EclipseAppLauncher.java:110)
at
org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.start(EclipseAppLauncher.java:79)
at
org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:369)
at
org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:179)
at
sun.reflect.NativeMethodAccessorImpl.invoke0(Native
Method) at
sun.reflect.NativeMethodAccessorImpl.invoke(Unknown
Source) at
sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown
Source) at
java.lang.reflect.Method.invoke(Unknown
Source) at
org.eclipse.equinox.launcher.Main.invokeFramework(Main.java:619)
at
org.eclipse.equinox.launcher.Main.basicRun(Main.java:574)
at
org.eclipse.equinox.launcher.Main.run(Main.java:1407)
at
org.eclipse.equinox.launcher.Main.main(Main.java:1383)

Please help me to resolve this error. Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

浅紫色的梦幻 2024-10-29 18:45:39

为了解决这个问题,我所要做的就是删除以下导入:

import kankan.wheel.R; // remove this line

复制并粘贴代码时,它会自动添加

当您从示例演示删除此屏幕抓取中突出显示的行

To solve this problem all I had to do is remove the following import:

import kankan.wheel.R; // remove this line

which gets added automatically when you copy and paste the code from the sample demo

Remove the line highlighted in this screen grab

我们只是彼此的过ke 2024-10-29 18:45:39

有一个资源没有找到。例如,它可能是您正在使用的 @drawable/layout_bg

如果不是这样,请看一下这两行:

initResourcesIfNecessary(WheelView.java:427) 
calculateLayoutWidth(WheelView.java:482) 

您正在调用一些正在请求不存在的资源(ID、可绘制对象等)的东西。

There is a resource that is not found. It could be e.g. the @drawable/layout_bg you're using?

If that's not it, take a look at these two lines:

initResourcesIfNecessary(WheelView.java:427) 
calculateLayoutWidth(WheelView.java:482) 

You're calling something there that is requesting a Resource that is not there (an Id, a drawable, etc etc).

把时间冻结 2024-10-29 18:45:39

每件事都是正确的。请检查drawable文件夹中layout_bg的图像格式。
支持的格式:

1.jpg
2.png
3.bmp

(例如:Android 接受 .jpg,而不是 .jpeg)。

Every thing is correct. Please check the image format for layout_bg in drawable folder.
Supported formats:

1.jpg
2.png
3.bmp

(eg: Android accept .jpg, not .jpeg).

中二柚 2024-10-29 18:45:39

我通过“尝试捕获”方法中的资源访问解决了这个问题,

     initResourcesIfNecessary

我知道这不是理想的解决方案,但至少现在我可以在设计时编辑我的布局。

修改后的方法如下

        private void initResourcesIfNecessary() {


    if (centerDrawable == null) {
        try {
            centerDrawable = getContext().getResources().getDrawable(R.drawable.wheel_val);
        } catch (Exception ex) {
            centerDrawable = null;
        }
    }

    if (topShadow == null) {
        try {
            topShadow = new GradientDrawable(Orientation.TOP_BOTTOM, SHADOWS_COLORS);
        } catch (Exception ex) {
            topShadow = null;
        }
    }

    if (bottomShadow == null) {
        try {
            bottomShadow = new GradientDrawable(Orientation.BOTTOM_TOP, SHADOWS_COLORS);
        } catch (Exception ex) {
            bottomShadow = null;
        }
    }
    try {
        setBackgroundResource(R.drawable.wheel_bg);
    } catch (Exception ex) {
        setBackgroundResource(0);
    }
}

I solved this by "try-catching" the resources access in the method

     initResourcesIfNecessary

I understand that it is not the ideal solution, but at least now i can edit my layouts in design time.

The modified method is below

        private void initResourcesIfNecessary() {


    if (centerDrawable == null) {
        try {
            centerDrawable = getContext().getResources().getDrawable(R.drawable.wheel_val);
        } catch (Exception ex) {
            centerDrawable = null;
        }
    }

    if (topShadow == null) {
        try {
            topShadow = new GradientDrawable(Orientation.TOP_BOTTOM, SHADOWS_COLORS);
        } catch (Exception ex) {
            topShadow = null;
        }
    }

    if (bottomShadow == null) {
        try {
            bottomShadow = new GradientDrawable(Orientation.BOTTOM_TOP, SHADOWS_COLORS);
        } catch (Exception ex) {
            bottomShadow = null;
        }
    }
    try {
        setBackgroundResource(R.drawable.wheel_bg);
    } catch (Exception ex) {
        setBackgroundResource(0);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文