Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/216.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
无法解析方法setText(java.lang.String)_Java_Android_Android Studio - Fatal编程技术网

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

无法解析方法setText(java.lang.String),java,android,android-studio,Java,Android,Android Studio,我已经研究了其他类似于我所遇到的错误的问题,但我似乎无法解决这里的问题。我所要做的就是单击一个按钮并更改TextView的文本 我的代码如下: public class FindBeerActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layo

我已经研究了其他类似于我所遇到的错误的问题,但我似乎无法解决这里的问题。我所要做的就是单击一个按钮并更改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(字符串)

我正在按照一个指南写信,我看不出我在哪里犯了错误,所以如果你们能帮我,我将不胜感激!谢谢!

你们可以改变

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

更改

brands.setText(beerType);

无论采用哪种方法,您都需要
TextView
引用上调用
setText
方法

您正在视图对象上尝试setText()方法。您需要在TextView对象上调用它。因此,要么将品牌声明更改为TextView,要么在调用setText()之前将您的品牌对象包装为TextView越过它。

改变

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


是的,这是个错误。我不知道为什么我把它创建为视图而不是文本视图!谢谢!
((TextView)brands).setText(beerType);
View brands = (TextView) findViewById(R.id.brands);
TextView brands = (TextView) findViewById(R.id.brands);