无法设置文字

时间:2012-03-13 08:33:28

标签: android string button int settext

我需要将Text设置为变量,我收到错误:

  

无法在基本类型int

上调用setText(String)

这是我的代码:

    public class CalcActivity extends Activity implements OnClickListener {
/** Called when the activity is first created. */
    private int mCount = 0;
    @Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

}
    public void onClick(View v) {
          // TODO Auto-generated method stub
    switch(v.getId())
          {
          case R.id.CountBtn:
               mCount++;
           R.id.TextViewCount.setText("You clicked " + mCount + "times");
           break;
          case R.id.ResetBtn:
           // Do something
           break;
          }
         }  
}

2 个答案:

答案 0 :(得分:6)

你不能在id(setText)上调用R.id.TextViewCount,因为它是一个int,但在视图上。替换这个:

R.id.TextViewCount.setText("You clicked " + mCount + "times");

用这个:

TextView v = (TextView)findViewById(R.id.TextViewCount);
v.setText("You clicked " + mCount + "times");

答案 1 :(得分:0)

public class StackActivity extends Activity implements OnClickListener {
    /** Called when the activity is first created. */
    int mCount = 0;
    TextView t;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Button b = (Button) findViewById(R.id.button);
        t = (TextView) findViewById(R.id.textview);
           b.setOnClickListener(this);

}
public void onClick(View v) {
        // TODO Auto-generated method stub
    switch(v.getId())
      {
      case R.id.button:
           mCount++;
       t.setText("You clicked " + mCount + "times");
       break;
      case R.id.ResetBtn:
       // Do something
       break;
      }
     }  
    }
        }