Alessandra Amosso
Alessandra Amosso

Reputation: 351

Android Studio cannot resolve symbol textView

I'm completely new to Android Studio.

I have just installed it on Windows 10 and since I wanted to learn how to use it, I have started to follow the online guide (https://developer.android.com/training/basics/firstapp/starting-activity.html#Up).

I am literally following it step by step, but when it comes to this part of the code:

public class DisplayMessageActivity extends AppCompatActivity {

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

        // Get the Intent that started this activity and extract the string
        Intent intent = getIntent();
        String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);

        // Capture the layout's TextView and set the string as its text
        TextView textView = (TextView) findViewById(R.id.textView);
        textView.setText(message);
    }

the "textView" part after "findViewById" appears red and I get this error message: "cannot resolve symbol 'textView'".

This happens even if I have imported android.widget.TextView.

Maybe this is stupid question, but I am completely new to Android Studio. Thanks for the answers :)

Upvotes: 2

Views: 16352

Answers (6)

Onkar charkupalli
Onkar charkupalli

Reputation: 1

After tiresome searches and updating settings, I Just clicked Invalidate Catches -> Invalidate And Restart And Done! TextView Error Disappearedenter image description here

Upvotes: 0

aman
aman

Reputation: 11

Erase the statement, then retype it. There is going to be a small pop up indicating you to press a command. It is basically an import library issue when you press the command (for macOS it is ⌥⏎ ) the respective library will be added. If this does not work, close Android Studio and reopen it . I am not an expert on this matter, but this worked for me hence I am sharing.

Upvotes: 0

trafernolio
trafernolio

Reputation: 1

The TextView object of the Activity_display_message is called TextView2, instead of Textview. So I tried and changed:

TextView textView = (TextView) findViewById(R.id.textView);

for

TextView textView = (TextView) findViewById(R.id.textView2);

And it worked!

Upvotes: 0

In my case, the problem was for other reason although the error was the same. I copied the following code out of the OnCreate method.

    // Get the Intent that started this activity and extract the string
    Intent intent = getIntent();
    String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);

    // Capture the layout's TextView and set the string as its text
    TextView textView = findViewById(R.id.textView);
    textView.setText(message);

The text (TextView) before findViewById was not neccesary.

Upvotes: 0

MicScoPau
MicScoPau

Reputation: 66

As mentioned by buutqn

<TextView> tag must have android:id="@+id/textView"

This is in the file: app.res.layout.activity_display_message.xml under the Text tab(as opposed to Design tab).

This issue can be duplicated by deleting the TextView box from the design tab, and inserting a new (second) text view.

Upvotes: 1

Rodrigo Butzke
Rodrigo Butzke

Reputation: 448

In your file R.layout.activity_display_message, the <TextView> tag must have android:id="@+id/textView"

Can you include your XML File?

Upvotes: 4

Related Questions