我已经看过类似于我得到的错误的其他问题,但我似乎无法在这里弄清楚这个问题.我要做的就是单击一个按钮并更改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); } }
和XML
当我构建我得到:"错误:找不到符号方法setText(String)
我正在按照教程写信,我看不出我犯了哪个错误,所以如果你们能帮助我,我会很感激!谢谢!
你可以改变
View brands = (TextView) findViewById(R.id.brands);
至
TextView brands = (TextView) findViewById(R.id.brands);
或改变
brands.setText(beerType);
至
((TextView)brands).setText(beerType);
无论哪种方式,您都需要在引用上调用该setText
方法TextView
.