Java 尝试在当前时间之后执行事件并将时间设置为相等时计时器类出错
我只想扫描我的时间何时等于当前时间,并使用计时器进行扫描,但当单击复选框且我的时间等于当前时间时,会出现致命错误。还有其他解决办法吗Java 尝试在当前时间之后执行事件并将时间设置为相等时计时器类出错,java,android,timer,alarm,Java,Android,Timer,Alarm,我只想扫描我的时间何时等于当前时间,并使用计时器进行扫描,但当单击复选框且我的时间等于当前时间时,会出现致命错误。还有其他解决办法吗 privae int currentHourOfDay,currentMinutes; private int mHourOfDay, mMinutes; ..... chk1 = (CheckBox) findViewById(R.id.checkBox1); chk1.setOn
privae int currentHourOfDay,currentMinutes;
private int mHourOfDay, mMinutes;
.....
chk1 = (CheckBox) findViewById(R.id.checkBox1);
chk1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (chk1.isChecked()) {
h = tv.getText().toString(); //get text from hours input
m = tv2.getText().toString(); //get text from miutes input
mHourOfDay = Integer.parseInt(h);
mMinutes = Integer.parseInt(m);
System.out.println(mHourOfDay);
System.out.println(mMinutes);
final Timer timer = new Timer();
timer.schedule( new TimerTask() {
public void run() {
Calendar cal = Calendar.getInstance();
currentHourOfDay = cal.get(Calendar.HOUR_OF_DAY);
currentMinutes = cal.get(Calendar.MINUTE);
System.out.println(currentHourOfDay);
System.out.println(currentMinutes);
Integer x = mHourOfDay;
Integer y = currentHourOfDay;
Integer x1 = mMinutes;
Integer y1 = currentMinutes;
if (x.equals(y) && x1.equals(y1)) {
Toast toast = Toast.makeText(getApplicationContext(),
"Now it equels!!", Toast.LENGTH_SHORT);
toast.show();
timer.cancel();
}
}}, 0, 1000);
} else {
System.out.println("alarm is off");
}
}
});
Mb我必须使用其他一些方法来解决我的任务,当我的时间等于当前时间时,执行一些事件?在ui线程上更新ui。在ui线程上显示toast消息 只需在下面显示toast的位置添加
runOnUiThread(new Runnable() //update ui here
{
public void run()
{
Toast.makeText(getApplicationContext(),
"Now it equels!!", Toast.LENGTH_SHORT).show();
}
});
计时器任务示例
_t.scheduleAtFixedRate( new TimerTask() {
@Override
public void run() {
//do something
runOnUiThread(new Runnable() //update ui here
{
public void run()
{
//display toast here.
}
});
}
}, 1000, 1000 );
用于显示来自TimerTask ThreadFATAL EXCEPTION:Timer-0的Toast,在该FATAL EXCEPTION之后,它还会显示其他内容吗?如果您需要我们的帮助,请包括完整的堆栈跟踪。