Ryan Travers
Ryan Travers

Reputation: 11

Why can't I resolve symbol "setText" in Android Studio?

I've been using the Android developer's "build your first app" tutorial and got stuck with creating the second activity. I'm new to Java as I've only coded in Python and C++ before so I am also quite rusty with the syntax and understanding everything in general. I've followed the tutorial up until this point in the code and got this:

package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;

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 = findViewById(R.id.textView);
    textView.setText(message);
}

The problem when I run the code is that it cannot resolve the symbol "setText" and not so why and what I have to do to overcome this. It also says "unknown class: 'message' ". Does anyone have ideas to help me?

Upvotes: 0

Views: 92

Answers (1)

Naeem
Naeem

Reputation: 514

Try this:

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 = findViewById(R.id.textView);
        textView.setText(message);
    }
}

Upvotes: 1

Related Questions