您好,我为我的游戏创建了一个计时器。问题是我在 2 个类中创建。这不起作用。我有一个例外。
public class Crono1 extends Activity implements Callback{
public TextView txtTiempo;
public boolean detenido;
public boolean pausado;
private Handler handler;
String cronometro;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
txtTiempo = (TextView) findViewById(R.id.ConTiempo);
final Button btn = (Button) findViewById(R.id.BtnAccion);
final Button btnDetener = (Button) findViewById(R.id.BtnDetener);
detenido = false;
pausado = true;
handler = new Handler(this);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Tiempo w = new Tiempo (handler);
Thread t = new Thread (w);
t.start();
}
});
btnDetener.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
detenido = true;
txtTiempo.setText("00:00:00:00");
}
});
}
@Override
public boolean handleMessage(Message msg) {
TextView texto = (TextView) findViewById(R.id.ConTiempo);
Integer text = (Integer) msg.obj;
texto.setText(text+"");
Log.d("ENTRO",msg.obj.toString());
return true;
}
public class Time implements Runnable{
public TextView txtTiempo;
public boolean detenido;
public boolean pausado;
int centesimas = 00,minutos=00, segundos=00, horas=00;
String cronometro;
private Handler handler;
public Time (Handler handler){
this.handler = handler;
}
@Override
public void run (){
while(!detenido)
{
while(!pausado)
{
try {
//if(centesimas == 99){
//centesimas = 00;
segundos++;
//}
if (segundos == 59) {
segundos = 00;
minutos++;
}
if (minutos == 59) {
minutos = 00;
//horas++;
}
centesimas++;
//cronometro = /*horas + ":" +*/ minutos + ":" + segundos /*+ ":" + centesimas*/;
sendMsg (minutos + ":" + segundos);
handler.sendEmptyMessage(0);
Thread.sleep(900);
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
private void sendMsg(String string)
{
Message message = new Message ();
message.obj = string;
handler.sendMessage(message);
}
Exepction 是 "Unchaught handler" 和 "ClassCastExeption"。我的错误是什么?以及如何做:如果时间是 1:50,完成应用程序或使用 Intent?