![](/img/trans.png)
[英]Cannot resolve method 'setText(java.lang.String[] with ResultView
[英]Cannot resolve method setText(java.lang.String)
我已經看過類似於我得到的錯誤的其他問題,但我似乎無法在這里弄清楚這個問題。 我要做的就是單擊一個按鈕並更改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
<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" />
當我構建我得到:“錯誤:找不到符號方法setText(String)
我正在按照教程寫信,我看不出我犯了哪個錯誤,所以如果你們能幫助我,我會很感激! 謝謝!
你可以改變
View brands = (TextView) findViewById(R.id.brands);
至
TextView brands = (TextView) findViewById(R.id.brands);
或改變
brands.setText(beerType);
至
((TextView)brands).setText(beerType);
無論哪種方式,您都需要在TextView
引用上調用setText
方法 。
您正在嘗試視圖對象上的setText()方法。 你需要在TextView對象上調用它。所以要么將品牌聲明更改為TextView,要么在調用setText()之前將你的品牌對象包裝到TextView。
更改
View brands = (TextView) findViewById(R.id.brands);
至
TextView brands = (TextView) findViewById(R.id.brands);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.