4

I'm new to Android Studio and Android development. So I was following the tutorial given by developer.android.com and I'm having an error in this line : EditText editText = (EditText) findViewById(R.id.editText); The error is saying that : cannot find symbol variable editText .

This is part of my code :

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


public class MainActivity extends AppCompatActivity {
   
    public void sendMessage(View view){
        EditText editText = (EditText) findViewById(R.id.editText);
        String message = editText.getText().toString();

    }
}
3
  • editText is not part of activity_main Commented Jul 28, 2017 at 15:50
  • 1
    please copy the complete compiler error-message to the question
    – TmTron
    Commented Jul 28, 2017 at 15:50
  • 1
    add your xml file Commented Jul 28, 2017 at 15:51

6 Answers 6

11

Make sure that you have assigned the id for your EditText in the activity_main.xml as below:

<EditText
    android:id="@+id/editText"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />
1
  • Just hit this same error! For me, it occurred when the I made a mistake trying to type in a new name over the default name of the widget. The Android tutorial has been impressive so far, but I wish they had put in an explanation of the line in code in question rather than skipping over it. That would have made it a bit easier to figure out what was going wrong. Commented Jul 21, 2020 at 7:03
1

You are probably missing the edit text in your XML. If not, perhaps the id you gave it differs from editText.

<EditText
    android:layout_width="368dp"
    android:layout_height="wrap_content"
    android:id="@+id/editText" />
0

Without posting your XML code it leaves it a bit difficult, however I would guess if you go back to your XML code where you have the edit text xml code. Add this android:id="@+id/editText".

0

I had the same issue as you OP. I went back to the activity_main.xml and noticed the ID was different somehow in the top right corner under attributes for the Text box. Changed the ID back and reran the app and no issues/

0

The reason is that you need to write editTextTextPersonName2 instead of editText to match the definition in activity_main.xml:

android:id="@+id/editTextTextPersonName2"

The code shall thus be as follows:

public void sendMessage(View view) 
{
    Intent intent = new Intent(this, DisplayMessageActivity.class);
    EditText editText = (EditText) findViewById(R.id.editTextTextPersonName2);
    String message = editText.getText().toString();
    intent.putExtra(EXTRA_MESSAGE, message);
    startActivity(intent);
}
0

R.id.editTextTextPersonName2 cause problem. You can try R.id.editText and follow the recommendation mentioned by others by adding android:id="@+id/editText" in activity_main.xml

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.