setText - 无法解析方法

问题描述:

很确定这是一个愚蠢的问题,但我无法弄清楚这一点。setText - 无法解析方法

我想获取一个由datepicker返回的字符串的整数。此代码的工作,其中day感兴趣

dateButton = (Button) findViewById(R.id.dateButton); 
dateButton.setText((Integer.toString(day)); 

此代码让我无法解析的方法的setText

String yearString = ""; 
yearString.setText(Integer.toString(year)); 

错误的整数,我不明白为什么我不能整型转换为字符串除非我使用视图?

+0

请注意 – 2014-10-02 12:09:01

+0

使用Integer.valueOf() – santalu 2014-10-02 12:11:04

+0

什么是yearString在这里? – 2014-10-02 12:13:02

是这样的,你想要做什么。

int year = 2014; 
String yearString = Integer.toString(year); 

因为stText方法仅用于在android likeTextView,EditText,Button的某些视图上设置文本。

dateButton = (Button) findViewById(R.id.dateButton); 
dateButton.settext(Interger.Valueof(day)); 
+0

Datebutton不是问题。它在我的代码中工作。 yearString是问题 – EnduroDave 2014-10-02 12:32:46

+0

String yearString =“”; yearString.setText(Integer.toString(yearString));因为你正在将字符串传递给tostring(),这在java中是不允许的。在这里输入转换错误,你正在做整数。toString这意味着tostring方面INteger不是字符串 – koutuk 2014-10-04 06:23:12

如果一天是一个整数值,您可以通过以下方式设置整数值,

dateButton.setText(day+""); 

dateButton.setText(String.valueOf(day)); 

dateButton.setText(Integer.toString(day)); 
+0

所有这些工作在datebutton上,但在yearString上引发错误,这是我的问题 – EnduroDave 2014-10-02 12:30:45

+0

yearString的值是什么?它是和整数或字符串?如果它是空的,那么在设置之前,验证一次。如果它为空,则设置空白字符串。 – Beena 2014-10-02 12:32:58

+0

看到我上面的编辑 - 同样的问题 – EnduroDave 2014-10-02 12:59:36

而不是

dateButton.setText((Integer.toString(day)); 

试试这个

dateButton.setText(day+""); 

你必须实例化对象调用之前查看我是新来的,这也 所以这

((TextView) findViewById(R.id.now_playing_text)).setText(trackTitle) 

变为

TextView Title = (TextView) findViewById(R.id.now_playing_text); 
Title.setText(trackTitle); 

的setText必须在一个包含在一个类应用它或晚餐类的setText方法。