relativelayout“无法解析资源...”安卓

发布于 2024-12-19 13:13:09 字数 1124 浏览 3 评论 0原文

使用以下代码时,我收到错误“无法解析资源@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 技术交流群。

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

发布评论

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

评论(7

(り薆情海 2024-12-26 13:13:09

我的代码没有这个错误。

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

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

塔塔猫 2024-12-26 13:13:09

正如 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

Menu Selection
enter image description here

Wait until AS has completely restarted and rebuilt. Hopefully it'll behave logically.

独﹏钓一江月 2024-12-26 13:13:09

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

RelativeLayout “Couldn't resolve resource… ... you need to define String Resources in res/values/strings.xml

单身狗的梦 2024-12-26 13:13:09

当我从其他设备导入项目时,出现同样的错误。
有时,此错误的原因是您当前的开发环境中的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.

enter image description here

You need change your local android SDK path.

1 Open local.properties

enter image description here

2 Replace the sdk.dir path use your local SDK path.

enter image description here

3 Refresh your project and Close your Android Studio.

enter image description here

4 Open the Android Studio, Everything is OK.

enter image description here

半﹌身腐败 2024-12-26 13:13:09

在装有 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)
Resync

难得心□动 2024-12-26 13:13:09

有时 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.

っ左 2024-12-26 13:13:09

当您忘记保存 /res/values/strings.xml 时会发生这种情况

This happens when you forget to save the /res/values/strings.xml

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文