无法解析方法setText(java.lang.String)

jbl*_*tle 5 java android android-studio

我已经看过类似于我得到的错误的其他问题,但我似乎无法在这里弄清楚这个问题.我要做的就是单击一个按钮并更改TextView的文本.

我的代码如下:

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

  }
}
Run Code Online (Sandbox Code Playgroud)

和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" />
Run Code Online (Sandbox Code Playgroud)

当我构建我得到:"错误:找不到符号方法setText(String)

我正在按照教程写信,我看不出我犯了哪个错误,所以如果你们能帮助我,我会很感激!谢谢!

Moh*_*uag 8

你可以改变

View brands = (TextView) findViewById(R.id.brands);
Run Code Online (Sandbox Code Playgroud)

TextView brands = (TextView) findViewById(R.id.brands);
Run Code Online (Sandbox Code Playgroud)

改变

brands.setText(beerType);
Run Code Online (Sandbox Code Playgroud)

((TextView)brands).setText(beerType);
Run Code Online (Sandbox Code Playgroud)

无论哪种方式,您都需要引用调用该setText方法TextView.