relativelayout“无法解析资源...”安卓
使用以下代码时,我收到错误“无法解析资源@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>
When using the below code, I get an error "Couldn't resolve resource @id/item1" Why is this? id/item1 is added before I use it, so I'm not sure why this is coming up.
<?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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
我的代码没有这个错误。
确保其他资源(布局、值、可绘制对象等)中没有错误。然后清理/刷新/构建项目
I don't have this error with your code.
Make sure you don't have errors in other resources (layouts, values, drawables, etc.). And then clean/refresh/build the project
正如 Jose 所说,有时 Android Studio 会变得疯狂。但重新启动并不总是足够的。
使缓存无效并重新启动。
文件->使缓存无效/重新启动... ->无效并重新启动
等待 AS 完全重新启动并重建。希望它的行为符合逻辑。
As Jose said, sometimes Android Studio just gets crazy. But a restart ain't always enough.
Invalidate your caches and restart.
File -> Invalidate Caches / Restart... -> Invalidate and Restart
Wait until AS has completely restarted and rebuilt. Hopefully it'll behave logically.
relativelayout“无法解析资源......您需要在 res/values/strings.xml 中定义字符串资源
RelativeLayout “Couldn't resolve resource… ... you need to define String Resources in res/values/strings.xml
当我从其他设备导入项目时,出现同样的错误。
有时,此错误的原因是您当前的开发环境中的android SDK路径错误。
您需要更改本地 Android SDK 路径。
1 打开 local.properties
2 使用本地 SDK 路径替换 sdk.dir 路径。
3 刷新您的项目并关闭 Android Studio。
4 打开Android Studio,一切正常。
I have the same error when I import a project form other devices.
Sometimes, the reason of this error is the wrong android SDK path in your current developing environment.
You need change your local android SDK path.
1 Open local.properties
2 Replace the sdk.dir path use your local SDK path.
3 Refresh your project and Close your Android Studio.
4 Open the Android Studio, Everything is OK.
在装有 Android Studio 的 2 台计算机上出现此问题。帮助打开 Gradle 并重新同步项目(如之前发布的屏幕截图所示)

Had this problem on 2 computers with Android Studio. Helped to open Gradle and re-sync the project (shown on the screenshot posted previously)

有时 IDE 会变得疯狂。 :) 只需重新启动 Android Studio,正如 @JasonAxelson、@AlexLockwood、@craned 和 @PhilippGächter 之前所说。
Some times the IDE gets crazy. :) Just restart the Android Studio, as said before by @JasonAxelson, @AlexLockwood, @craned and @PhilippGächter.
当您忘记保存 /res/values/strings.xml 时会发生这种情况
This happens when you forget to save the /res/values/strings.xml