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

时间:2015-12-02 18:30:24

标签: java android android-studio

我已经查看过类似我遇到的错误的其他问题,但我似乎无法在这里找出问题所在。我所要做的就是点击按钮并更改TextView的文字。

我的代码如下:

org.hibernate.hql.internal.ast.InvalidPathException: Invalid path: 'ranking.seller'

org.hibernate.hql.internal.ast.InvalidPathException: Invalid path: 'ranking.point'

org.hibernate.hql.internal.ast.InvalidPathException: Invalid path: 'ranking.rank'

org.hibernate.hql.internal.ast.InvalidPathException: Invalid path: 'old.rank'

java.lang.IllegalArgumentException: org.hibernate.hql.internal.ast.QuerySyntaxException: Invalid path: 'ranking.seller'

和XML

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

  }
}

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

我跟着这封信的教程,我无法看到我犯了哪个错误,所以如果你们能帮助我,我会很感激!谢谢!

3 个答案:

答案 0 :(得分:8)

您可以更改

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

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

更改

brands.setText(beerType);

((TextView)brands).setText(beerType);

无论哪种方式,您都需要setText参考上调用TextView方法。

答案 1 :(得分:0)

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

答案 2 :(得分:0)

更改

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

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