我的应用程序强制关闭,但是编译器根本没有显示任何错误,我已经提供了代码
package my.android;
package com.google.android.gms.auth.sample.helloauth;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
import java.util.Timer;
import java.util.TimerTask;
public class HelloActivity extends Activity {
/** Called when the activity is first created. */
int time;
TextView tv;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
time = 0;
Timer t = new Timer();
t.scheduleAtFixedRate(new TimerTask() {
public void run() {
runOnUiThread(new Runnable() {
public void run() {
tv = (TextView) findViewById(R.id.tvDisplay);
tv.setText(time);
time += 1;
}
});
}
}, 0, 10000);
}
}发布于 2014-01-23 12:04:43
SetText方法请求一个字符串参数。尝尝这个
tv.setText(String.valueOf(time));发布于 2014-01-23 15:21:36
在创建timer after setContentView(R.layout.main);的对象之前移动此行
tv = (TextView) findViewById(R.id.tvDisplay);和改变
tv.setText(time); 至
tv.setText(String.valueOf(time));因为setText()总是将String作为参数传递给int,这就是强制关闭的原因
https://stackoverflow.com/questions/21298660
复制相似问题