Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

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();

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" />
                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.
– Phil Freihofner
                Jul 21, 2020 at 7:03

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" />

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);
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.