5

I've looked at other questions similar to the error I'm getting, but I can't seem to figure out the issue here. All I'm trying to do is click a button and change a TextView's text.

My code is as follows:

public class FindBeerActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_find_beer);
}
//call when the user clicks the button
public void onClickFindBeer(View view) {

    //Get a reference to the TextView
    View brands = (TextView) findViewById(R.id.brands);

    //Get a reference to the Spinner
    Spinner color = (Spinner) findViewById(R.id.color);

    //Get the selected item in the Spinner
    String beerType = String.valueOf(color.getSelectedItem());

    //Display the beers. This is where the error is
    brands.setText(beerType);

  }
}

and the XML

 <TextView
    android:id="@+id/brands"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@id/find_beer"
    android:layout_alignStart="@id/find_beer"
    android:layout_below="@id/find_beer"
    android:layout_marginTop="18dp"
    android:text="@string/brands" />

When I build I get: "error: cannot find symbol method setText(String)

I'm following a tutorial to the letter and I can't see where I've made a mistake, so I'd appreciate it if you guys could help me out! Thanks!

3 Answers 3

8

You can either change

View brands = (TextView) findViewById(R.id.brands);

to

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

OR change

brands.setText(beerType);

to

((TextView)brands).setText(beerType);

In either ways, you need to call the setText method on a TextView reference.

Sign up to request clarification or add additional context in comments.

1 Comment

Yup, there's the mistake. I'm not sure why I created that as a View and not a TextView! Thanks!
0

You are trying setText() method on a view object. You need to call it on a TextView object.so either change the declaration of brands to TextView or just wrap ur brands object to TextView before calling setText() over it.

Comments

0

Change

View brands = (TextView) findViewById(R.id.brands);

To

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.