2

我使用一些自定义组件创建了 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。

请帮我解决这个错误。提前致谢。

4

4 回答 4

2

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

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

当您复制并粘贴示例演示中的代码时,它会自动添加

删除此屏幕抓取中突出显示的行

于 2012-11-15T20:03:50.583 回答
1

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

1.jpg 2.png 3.bmp

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

于 2011-03-15T13:55:50.263 回答
1

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

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

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

您在那里调用的东西正在请求不存在的资源(Id、drawable 等)。

于 2011-03-15T11:54:44.160 回答
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);
    }
}
于 2012-09-17T13:41:51.220 回答