0

我想做一个简单的计数器。当我点击屏幕时,textvalue 的值会增加。

所以我的活动。

public class MyTasbih extends Activity implements View.OnClickListener {

//Button btn;
TextView t;
int i=0;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    //btn=(Button)findViewById(R.id.button);
    t=(TextView)findViewById(R.id.t);

    //btn.setOnClickListener(this);
    t.setOnClickListener(this);

    updateCounter();

}

public void onClick(View view) {
    i++;
    updateCounter();

}

private void updateCounter() {

    t.setText(i);
}
}

但它的崩溃。我是新手,请帮忙。

4

3 回答 3

1

请试试这个并检查

private void updateCounter() {

    t.setText(String.valueOf(i));
}
于 2012-05-23T06:42:32.180 回答
0
public class MyTasbih extends Activity implements View.OnClickListener {

//Button btn;
TextView t;
int i=0;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main6);

    //btn=(Button)findViewById(R.id.button);
    t=(TextView)findViewById(R.id.t);

    //btn.setOnClickListener(this);
    t.setOnClickListener(this);

    updateCounter();

}

public void onClick(View view) {
    i++;
    updateCounter();

}

private void updateCounter() {

    t.setText(String.valueOf(i));
}
}
于 2012-05-23T06:48:26.473 回答
0

我没有方便的 eclipse 来检查这个,但不应该更新:

private void updateCounter() {
    t.setText(Integer.toString(i));
} 
于 2012-05-23T06:41:11.643 回答