[Solved] Android tutorial “Cannot resolve symbol ‘setText’ “


You have to place these lines:

Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);

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

Inside onCreate (or some other method). You can’t place code like that outside methods in Java. I say onCreate because you load views. That should be called from onCreate some or another way, but an external method called from onCreate would also do

So your code would be:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_display_message);
    Intent intent = getIntent();
    String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);

    TextView textView = findViewById(R.id.textView);
    textView.setText(message);
}

And this applies to everything. The more or less only thing you can do outside methods is declare variables. Setting those variables, changing them (through methods like setText in the case of textview) has to be done inside a method.

1

solved Android tutorial “Cannot resolve symbol ‘setText’ ”