"即使名称匹配,也找不到与给定名称匹配的资源"错误

Onu*_*bek 7 xml android gradle

我正在构建一个简单的待办事项列表应用程序.我试图通过将它们相对于彼此定位来组织我的布局.我通过调用XML属性来做到这一点android:layout_toStartOf="@id/to_do_btn"但是我收到了一个No Resource Found That Matches The Given Name错误.我基本上尝试了Stackoverflow上的每个解决方案但没有任何帮助.这是我的XML:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="myapp.onur.todo.MainActivity">

    <ListView
        android:id="@+id/to_do_list"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_alignParentStart="true"
        android:layout_above="@id/to_do_btn"
        android:layout_margin="5dp"
        >
    </ListView>

    <EditText
        android:id="@+id/to_do_editText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:hint="Enter Something To Do"
        android:layout_toStartOf="@id/to_do_btn"
        android:layout_alignParentBottom="true"
        android:layout_alignParentStart="true"
        android:layout_alignTop="@id/to_do_btn"
        android:layout_below="@id/to_do_list"
        android:layout_margin="5dp"
        />

    <Button
        android:id="@+id/to_do_btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="add item"
        android:layout_margin="5dp"
        android:layout_alignParentBottom="true"
        android:layout_alignParentEnd="true"
        />

</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

这是错误的一个例子: Error:(15, 31) No resource found that matches the given name (at 'layout_above' with value '@id/to_do_btn').

我不知道是怎么回事.如果它有帮助,这是我的gradle依赖项:

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support.constraint:constraint-layout:1.0.2'
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:26.0.0-alpha1'
}
Run Code Online (Sandbox Code Playgroud)

Pha*_*inh 16

更改

android:layout_above="@id/to_do_btn"
Run Code Online (Sandbox Code Playgroud)

android:layout_above="@+id/to_do_btn"
Run Code Online (Sandbox Code Playgroud)

或者像这样订购你的布局

<Button
        android:id="@+id/to_do_btn"
        ...
        />

<ListView
        ...
        android:layout_above="@id/to_do_btn"
        >
</ListView>
Run Code Online (Sandbox Code Playgroud)


Ben*_* P. 11

Phan Van Linh的回答是正确的,但这就是为什么它是正确的.

当你写作时@+id/foo,你会说"使用id foo,如果它不存在则创建它".当你写作时@id/foo,你会说"使用id foo".如果该id不存在,则这是一个错误(就像引用您尚未声明的Java变量一样).

如果你以相反的顺序放置布局,它"工作"的原因是现在你的<Button android:id="@+id/foo">标签首先出现,所以当处理文件时,会在处理foo之前创建id <ListView android:layout_above="@id/foo">.

事实上,如果你交换两个视图,构建你的项目,然后将它们交换回原始顺序,你会发现它仍然有效.这是因为生成的R.java文件是以增量方式构建的,因此id会foo从最后一次构建中继续存在.

但是如果对项目进行干净的重建,问题就会回来(因为R.java被破坏并重新生成).