定时器检查每小时是否有小时变化,如果是,那么按钮需要变为绿色并且可点击,否则它会保持红色且不可点击。
到目前为止,我收到此错误,我不知道如何处理: -
android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
Timer timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
CHECK();
}
}, 1*60*1000, 1*60*1000);
public void CHECK(){
sqliteDbHelper_ = new Sqlite_DBHelper(getActivity());
sqLiteDatabase = sqliteDbHelper_.getWritableDatabase();
String[] columns = {"hour_only","JAKOST"};
cursor = sqLiteDatabase.query("podatki", columns,null,null,null,null,null);
Calendar calander = Calendar.getInstance();
Integer currentHour = calander.get(Calendar.HOUR_OF_DAY);
buttonSaveOnMainPage.setClickable(true);
cursor.moveToLast();
if (cursor.moveToLast()== false){
lastInputedHour = 0;
}
else{
lastInputedHour = cursor.getInt(0);
}
Log.e(TAG+"zadnja vnesena URA",""+lastInputedHour);
Log.e(TAG+"trenutna URA",""+currentHour);
if (lastInputedHour == currentHour){
buttonSaveOnMainPage.setBackgroundResource(R.color.ColorRed);
buttonSaveOnMainPage.setClickable(false);
}
else
buttonSaveOnMainPage.setBackgroundResource(R.color.ColorGreenDark);
buttonSaveOnMainPage.setClickable(true);
}
答案 0 :(得分:0)
错误非常自我解释。在UI元素的OnClickListener()
内实现计时器。这是一个例子: -
Button1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//Some business logic
Update_ui();
if (!Player.isTuningnow) {
Button1.setImageResource(R.drawable.btn_pause_pressed);
Player.Paused = false;
new CountDownTimer(600, 1) {
public void onTick(long millisUntilFinished) {
}
public void onFinish() {
Button1.setImageResource(R.drawable.music_btn_play);
//Some more business logic
}
}.start();