简体   繁体   中英

Android Resource Compilation Error on Android Studio while compiling app

I am getting a compilation error in the XML file for the ID's made. The btnAdd, etItem, rvItems were in red until I made them a resource. I added the value Button, EditText, RecyclerView and I was not sure if that was the right move, but I did it and it worked. The text was not red anymore, but I was getting an error. After doing the changes I thought could work, it is not helping. The error seems to be in the small xml file. Originally the problem was that the btnAdd, etItem, rvItems were red and did not understand why. It suggested to add them as resources, so I did...

package com.example.simpletodo;

import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    List<String> items;

    Button btnAdd;
    EditText etItem;
    RecyclerView rvItems;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btnAdd = findViewById(R.id.btnAdd);
        etItem = findViewById(R.id.etItem);
        rvItems = findViewById(R.id.rvItems);


        etItem.setText("I'm doing this in Java.");

        items = new ArrayList<>();
        items.add("Buy Milk");
        items.add("tomatoes");
        items.add("potatoes");
    }
}

Here is the xml file which is where the problem is.

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <item name="btnAdd" type="id">Button</item>
    <item name="etItem" type="id">EditText</item>>
    <item name="rvItems" type="id">RecyclerView</item>
</resources>

This was created by me. The btnadd, etItem, and rvItems were in red, so I added them as a resource and it made the xml file above.

Here is my activity xml file.

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

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_alignParentBottom="true"
        android:layout_marginEnd="1dp"
        android:layout_marginRight="2dp"
        android:layout_marginBottom="-2dp"
        android:text="Add" />

    <EditText
        android:id="@+id/textView"
        android:layout_width="369dp"
        android:layout_height="39dp"
        android:layout_alignParentStart="true"
        android:layout_alignParentEnd="true"
        android:layout_alignParentBottom="true"
        android:layout_marginStart="0dp"

        android:layout_marginEnd="93dp"
        android:layout_marginBottom="0dp"
        android:hint="Add in item here"
        android:textAlignment="textStart" />

    <androidx.recyclerview.widget.RecyclerView
        android:layout_width="match_parent"
        android:layout_height="512dp" />

</RelativeLayout>

It's my first time doing this project, so please help在此处输入图像描述

Here is the xml made.

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <item name="btnAdd" type="id">Button</item>
    <item name="etItem" type="id">EditText</item>>
    <item name="rvItems" type="id">RecyclerView</item>
</resources>

It's my first time with this project and most questions out there are not helping me with my problem. Please help, I tried cleaning and rebuilding the project and when I press option enter I add them as resources. but gives me a compilation error. Please Help!

Try referring with @id

<Button
    android:id="@id/btnAdd" />

<EditText
    android:id="@id/etItem" />

<androidx.recyclerview.widget.RecyclerView
    android:id="@id/rvItems" />

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM