Components are not in class R

Asked

Viewed 464 times

2

I’m building an agenda, and for that I’ve created a Activity where would be a list of all contacts:

<LinearLayout 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"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".actContato">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <EditText
            android:id="@+id/edtPesquisa"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:ems="10"
            android:hint="@string/lbl_hint_nome_pesquisa"
            android:inputType="textPersonName" />

        <ImageButton
            android:id="@+id/imb_ADD"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@android:drawable/ic_menu_add" />

    </LinearLayout>

    <ListView
        android:id="@+id/lstContatos"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1" />

</LinearLayout>

And another Activity where all contacts would be registered:

<LinearLayout 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"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="aula4.com.example.raru.agenda.ActContatos">

    <LinearLayout
        android:id="@+id/lnlNome"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <TextView
            android:id="@+id/txtNomeContato"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/lblNomeContato"
            android:textAppearance="?android:attr/textAppearanceMedium" />

        <EditText
            android:id="@+id/edtNome"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:ems="10"
            android:hint="@string/lbl_hint_nome"
            android:inputType="textPersonName" />

        <EditText
            android:id="@+id/edtSobreNome"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ems="10"
            android:hint="@string/lbl_hint_sobrenome"
            android:inputType="textPersonName" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/lnlTelefone"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:orientation="vertical">

        <TextView
            android:id="@+id/txtTelefone"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/lblTelefone"
            android:textAppearance="?android:attr/textAppearanceMedium" />

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="horizontal">

            <EditText
                android:id="@+id/edtTelefone"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:ems="10"
                android:inputType="phone" />

            <Spinner
                android:id="@+id/spnTelefone"
                android:layout_width="150dp"
                android:layout_height="wrap_content"
                android:layout_weight="1" />
        </LinearLayout>

    </LinearLayout>

    <LinearLayout
        android:id="@+id/lnlEmail"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:orientation="vertical">

        <TextView
            android:id="@+id/txtEmail"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/lblEmail"
            android:textAppearance="?android:attr/textAppearanceMedium" />

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="horizontal">

            <EditText
                android:id="@+id/edtEmail"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:ems="10"
                android:inputType="textEmailAddress" />

            <Spinner
                android:id="@+id/spnEmail"
                android:layout_width="150dp"
                android:layout_height="wrap_content"
                android:layout_weight="1" />
        </LinearLayout>
    </LinearLayout>

    <LinearLayout
        android:id="@+id/lnlEndereco"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:orientation="vertical">

        <TextView
            android:id="@+id/txtEndereco"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/lblEndereco"
            android:textAppearance="?android:attr/textAppearanceMedium" />

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="horizontal">

            <EditText
                android:id="@+id/edtEndereco"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:ems="10"
                android:inputType="textPostalAddress" />

            <Spinner
                android:id="@+id/spnEndereco"
                android:layout_width="150dp"
                android:layout_height="wrap_content"
                android:layout_weight="1" />
        </LinearLayout>
    </LinearLayout>

    <LinearLayout
        android:id="@+id/lnlDatasEspeciais"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:orientation="vertical">

        <TextView
            android:id="@+id/txtDatas"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/lblDatas"
            android:textAppearance="?android:attr/textAppearanceMedium" />

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="horizontal">

            <EditText
                android:id="@+id/edtDatas"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:ems="10"
                android:inputType="date" />

            <Spinner
                android:id="@+id/spnDatas"
                android:layout_width="150dp"
                android:layout_height="wrap_content"
                android:layout_weight="1" />
        </LinearLayout>
    </LinearLayout>

    <LinearLayout
        android:id="@+id/lnlGrupo"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:orientation="vertical">

        <TextView
            android:id="@+id/txtGrupos"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/lblGrupos"
            android:textAppearance="?android:attr/textAppearanceMedium" />

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="horizontal">

            <EditText
                android:id="@+id/edtGrupos"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:ems="10"
                android:inputType="text" />

        </LinearLayout>

    </LinearLayout>

</LinearLayout>

But when it comes to getting the reference in the java classes of both Activity (findViewById()), is pointed out that there is no reference to the xml components.

I took a look at the R class and there’s really no reference to them.

OBS: I remembered that this error only happened after editing the xml menu, before I could even perform tests by the virtual machine, I already removed all the modifications and left the xml as before:

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context="aula4.com.example.raru.agenda2.ActContatos">

    <item
        android:id="@+id/action_settings"
        android:orderInCategory="100"
        android:title="@string/action_settings"
        app:showAsAction="never" />
</menu>

and:

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context=".actContato">

    <item
        android:id="@+id/action_settings"
        android:orderInCategory="100"
        android:title="@string/action_settings"
        app:showAsAction="never" />
</menu>

Mistakes:

inserir a descrição da imagem aqui

inserir a descrição da imagem aqui

  • 1

    Checks for errors in your layout file and a clean one in the project

  • I had done this before asking, but the mistake always persists.

3 answers

1

The error is not in the generation of the R class or in the layout, but in the build.Gradle file, that is, your project build configuration file. The two images you posted clearly show that the error has nothing to do with the layout. Be sure to fix the errors indicated in the lower left panel of the second image and correctly write your build file. After everything is fixed, give a clean + rebuild on the project.

  • I fixed the mistake, I did the clean and rebuild, but it didn’t change at all.

1

Try the following, comment or remove all lines with errors, give a clean on your project and then build. The elements will likely reappear in the R class.

The R class is automatically generated having all the elements of its layouts in it, but in some types of errors in your code it may not be generated or not fully generated.

  • i tried, and actually the components were created correctly in class R. However the error still persists.

  • Are you sure that you are typing the name correctly or that there is no error in your layout?

  • I even copied and pasted the names, and in the layout I couldn’t find, I even posted here to see if anyone noticed anything.

  • Really strange, open your act_contact that is also not being found and print here in doubt

  • http://i.stack.imgur.com/9iIlV.png<br/>&#xA;&#xA;<br/>act_Contato Renderizada:&#xA;http://i.stack.imgur.com/UtJDU.png<br/>&#xA;act_Contatos Renderizada:&#xA;http://i.stack.imgur.com/XBjQw.png

  • So friend for what I saw your class actContact.java is with the wrong name because it should start with uppercase and in act_contacts.xml in Linearlayout you put in tools:context="aula4.com.example.raru.agenda.Actcontacts"> But from what I understand your project is class4.com.example.raru.agenda2 (Has a 2 at the end) Logo should be: tools:context="aula4.com.example.raru.agenda.Actcontacts"> Otherwise I don’t see other errors. See if this resolves. If yes, please mark the answer as useful.

  • Jean. the number 2 is why in the first attempt the error happened, and at the time I thought it should be some bug related to the IDE, so I created a second project (this one) and copied everything I had created. But the error persisted. I tried your tip and it also didn’t work.

  • Good friend, so I’ve exhausted my options. As a possible solution to proceed I recommend creating a new project and not copying the elements or copying but changing the name so that it is according to the context of the new project and not the old one.

Show 3 more comments

0

Try two things:

1º In Android Studio click on "Build" and then on "Clean Project" 2nd Click again on "Build" click on "Rebuild Project"

See if it solves

  • Unfortunately it didn’t. The only way out was to recreate the project.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.