• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Newbie question "Cannot resolve symbol setText"

Aap Beer

Lurker
Mar 21, 2017
6
2
Dear,

I'm new to Android/Java, trying to replicate the exercise from https://developer.android.com/training/basics/firstapp/index.html in Android Studio 2.3

After the last step "Start Another Activity" I get the error message "Cannot resolve symbol setText" in statement textView.setText(message);

Why?

Sorry for stupid question :)

Code as follows:
Code:
package acme.hello_world;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;

public class DisplayMessageActivity extends AppCompatActivity {

    [USER=1021285]@override[/USER]
    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);

}
 
Last edited by a moderator:
You have a block of code which is outside the method. It's a question of where you place the brackets "{ }". So your code should read

Code:
public class DisplayMessageActivity extends AppCompatActivity {
   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);
   }
}
 
Upvote 0
It's not incorrect. You just typed it wrong ;)

I used select, copy and paste :)
Cf. aforementioned webpage:

Code:
Add a method in MainActivity.java that's called by the button as follows:

In the file app > java > com.example.myfirstapp > MainActivity.java, add the sendMessage() method stub as shown below:
public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    /** Called when the user taps the Send button */
    public void sendMessage(View view) {
        // Do something in response to button
    }
}
 
Upvote 0
Ok, as you're persisting with this, let me explain in detail what's wrong with the code you posted. And I can really assure you that the example code posted on Google's official Android developer website is not wrong. If that were the case, then the mistake would have been spotted long ago.

Anyway, here's the code you posted:

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

And here's the code I just pasted from the website:

Code:
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);
}

Can you see the crucial difference in the placement of the closing "}"?
This makes all the difference, because the code in your case is just hanging in the middle of nowhere. Any code you write, which isn't a variable declaration, must be within the confines of a method body.
 
Upvote 0

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones