相对布局 "无法解析资源..." Android

21
使用以下代码时,我遇到了一个错误:“无法解析资源@id/item1”。这是为什么?在我使用它之前,已经添加了id/item1,所以我不确定为什么会出现这个问题。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <ImageView
        android:id="@+id/item1"
        android:layout_width="50dip"
        android:layout_height="50dip"
        android:src="@drawable/noimage" />
    <TextView         
        android:id="@+id/item2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Title"
        android:padding="3dip" 
        android:textSize="20dip"
        android:layout_toRightOf="@id/item1"/>
    <TextView         
        android:id="@+id/item3"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Content"
        android:padding="3dip" 
        android:textSize="20dip"
        android:layout_below="@id/item1"/>

</RelativeLayout>
7个回答

19

使用你的代码,我没有发现这个错误。

请确保其他资源(布局、值、可绘制等)中没有错误。然后清除/刷新/构建项目。


7
我也遇到了同样的错误,并通过重新启动eclipse解决了问题(清理并没有帮助)。尽管在我拖动东西时,图形相对布局编辑器崩溃了。 - Jason Axelson
11
当有疑问时......重启 Eclipse。这可能是解决我与 Android 相关问题的答案,占所有问题的一半 -_- - Alex Lockwood
3
对于那些使用Android Studio并且清理无效的人,重新启动也解决了我的问题。 - craned
1
我也遇到过这个问题,尝试了Build>Clean Project + Build>Make Project或者Build>Rebuild Project,但都没有帮助。重启Android Studio解决了这个问题。 - Philipp Gächter

14

如Jose所说,有时候Android Studio会出现奇怪的问题。但是重启并不总是足够的。

清除缓存并重启。

文件 -> 清除缓存 / 重启... -> 清除并重启

菜单选择 输入图片描述

等待AS完全重启和重新构建。希望它会变得更加合理。


1

RelativeLayout“无法解析资源...您需要在res/values/strings.xml中定义字符串资源。


0

当我从其他设备导入项目时,我遇到了相同的错误。 有时候,这个错误的原因是你当前开发环境中的Android SDK路径不正确。

enter image description here

你需要更改本地的Android SDK路径。

1. 打开local.properties文件

enter image description here

将sdk.dir路径替换为您本地的SDK路径。

enter image description here

刷新你的项目并关闭你的Android Studio。

enter image description here

4 打开Android Studio,一切顺利。

enter image description here


0

我在两台电脑上使用Android Studio时遇到了这个问题。打开Gradle并重新同步项目(如先前发布的屏幕截图所示)有所帮助。 Resync


0
有时候IDE会变得疯狂。 :) 正如@JasonAxelson、@AlexLockwood、@craned和@PhilippGächter之前所说的那样,只需重新启动Android Studio即可。

-1

当你忘记保存/res/values/strings.xml文件时,就会出现这种情况。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接