2011-03-15 74 views
2

我創建的Android XML使用一些自定義組件進行佈局。但是當我保存XML文件時出現錯誤。由於這是我與自定義組件(小工具)的第一次,我不知道爲什麼我得到這個錯誤。 這是我的xml代碼。無法使用自定義組件保存Android 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) 在 安德烈oid.widget.LinearLayout.measureChildBeforeLayout在 android.widget.LinearLayout.measureHorizo​​ntal(LinearLayout.java:619)在 android.widget.LinearLayout.onMeasure(LinearLayout.java:280)在(LinearLayout.java:888) 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) at 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) at com.android.ide.common.rendering.LayoutLibrary.createSession(LayoutLibrary.java:285) at com.androi d.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) 在 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) 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) 在 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) at org.eclipse.core.runtime.SafeRunner.run(SafeRunner.java:42) at 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) 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) 在 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) at org.eclipse.ui.internal.Workbench.runEventLoop(Workbench.java:2640) at 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) 在 有機.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(本地 法)維持在 java.lang.reflect.Method中 sun.reflect.NativeMethodAccessorImpl.invoke(未知 來源)在 sun.reflect.DelegatingMethodAccessorImpl.invoke(未知 來源) .invoke在 org.eclipse.equinox.launcher.Main.basicRun(Main.java:574)(未知 源)在 org.eclipse.equinox.launcher.Main.invokeFramework(Main.java:619) 在(Main.java:1407) at org.eclipse.equinox.launcher.main.main(Main.java:1383)

請幫我解決這個錯誤。提前致謝。

+0

您是否指定了fil像mylayout.xml一樣?然後你在xml文件的開頭忘了 - ><?xml version =「1.0」encoding =「utf-8」?>並在第一個佈局元素中聲明瞭命名空間 - > xmlns:android =「http:// schemas .android.com/apk/res/android「 – Rainer 2011-03-15 11:50:26

+0

修復了佈局,因此xml start實際上可見:) – Nanne 2011-03-15 11:53:11

+0

現在」xmlns:android ...「修復了嗎?也許你應該清理項目並重新構建它。 包「kankan.wheel.widget」中是否有WheelView組件? – Rainer 2011-03-15 11:55:00

回答

1

找不到資源。它可以是例如您正在使用的@drawable/layout_bg

如果這不是它,看看這兩條線:

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

你打電話的東西有沒有被請求的資源,是不存在的(一個ID,一個繪製,等等等等)。

+0

如果這是問題,那麼當他拿出他的自定義組件時它將不起作用。 – stealthcopter 2011-03-15 11:57:07

+0

我已將layout_bg添加到可繪製文件夾,並且它位於R.java文件中。 – Chrishan 2011-03-15 11:57:22

+0

@stealthcopter:是的,但我沒有閱讀評論時,我張貼。所以問題應該在WheelView.java文件的initResourcesIfnecessary中。 – Nanne 2011-03-15 11:58:26

1

每件事都是正確的。請在可繪製文件夾中檢查layout_bg的圖像格式。 支持格式:

1.JPG 2.png 3.bmp

(如:安卓接受.jpg,不.JPEG)。

+0

這是一個自定義佈局。不是圖像。 – Chrishan 2011-03-16 04:05:57

+0

這個自定義佈局是什麼? – Androider 2011-03-16 05:22:13

+0

這是自定義佈局中的代碼。 <形狀的xmlns:機器人= 「http://schemas.android.com/apk/res/android」> \t <梯度\t \t \t 機器人\t:startColor = 「#FF000000」 \t \t機器人:centerColor = 「#FF000000」 \t \t機器人:ENDCOLOR = 「#FF777777」 \t \t機器人:角= 「90」/> 通過我已經解決了我的問題的方式。將資源導入到我的項目中有一些問題。感謝幫助。 – Chrishan 2011-03-16 06:27:29

0

我解決了這個由"try-catching"的方法

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

爲了解決這個問題,我不得不做的是刪除以下導入:當您複製

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

它就會自動添加並粘貼代碼示例演示

Remove the line highlighted in this screen grab

+0

這種問題可能有多個根本原因,這裏沒有提到的一個根本原因是在自定義視圖初始化代碼中訪問資源 - 請參閱http://stackoverflow.com/questions/11543855/resourcesnotfoundexception-in-graphical-layout- ADT-預覽但-APP-實際上-WO/17742353#17742353 – Cel 2013-07-19 09:29:20