我已經看過類似於我得到的錯誤的其他問題,但我似乎無法找出這裏的問題。我想要做的只是點擊一個按鈕並更改TextView的文本。無法解析方法setText(java.lang.String)
我的代碼如下:
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(字符串)
我然後我看了一封教程,看不到我犯了什麼錯誤,所以如果你們能幫助我,我會很感激!謝謝!
是啊,有錯誤。我不知道爲什麼我將它創建爲視圖而不是TextView!謝謝! – jblittle
不客氣:) –