Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cannot resolve symbol 'editText'

Tags:

android

public void sendMessage(View view) {
    // Do something in response to button
    Intent intent = new Intent(this, DisplayMessageActivity.class);
    EditText editText = (EditText) findViewById(R.id.editText);
    String message = editText.getText().toString();
    intent.putExtra(EXTRA_MESSAGE, message);
    startActivity(intent);

I just started Android prog! so i am following this guide "Start Another Activity" fro google: https://developer.android.com/training/basics/firstapp/starting-activity.html Error occurs in this line EditText editText = (EditText) findViewById(R.id.editText); In the "(R.id.editText)" part! so if someone can explain why & how am getting this error it will be greately helpful! Thanks in advance!

like image 854
Anantha RamaKrishnan Avatar asked May 07 '17 04:05

Anantha RamaKrishnan


2 Answers

I'm coming to this late, but adding this for those who find this searching for the answer. Here's the heart of the "cannot resolve symbol 'editText'" problem. In activity_main.xml, when creating the text edit box, the editor may append a number to the id:

<EditText
    android:id="@+id/editText3"

If you either remove the number, or edit the reference in MainActivity.java to match it, it works.

Note, if you remove the number, then you need to also remove it (in the same file) at:

<Button
    ...
    app:layout_constraintBaseline_toBaselineOf="@+id/editText3"
    app:layout_constraintLeft_toRightOf="@+id/editText3"

as well, or the contraints won't connect.

Really something they should have noted in the tutorial.

On the flip side, I think I learned more from figuring this out than the rest of the tutorial so far.

like image 157
Chaosflow Avatar answered Oct 23 '22 12:10

Chaosflow


Because you have't imported fallowing

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
like image 28
Ali Ahsan Avatar answered Oct 23 '22 12:10

Ali Ahsan