我要做的是让我的程序在更改图像之前等待3秒钟。我做了我的研究,似乎这可以解决它,但它告诉我,我需要让c1和c2最终。但是,这会破坏我的其余代码。
我试图在3秒后运行这两行:
c1.getImg().setImageResource(R.drawable.facedown);
c2.getImg().setImageResource(R.drawable.facedown);
这是什么解决方案?谢谢!
private Thread thread;
public void checkMatch(int num) {
flips++;
President c1 = null;
President c2 = cards.get(num - 1);
if (flips % 2 != 0) {
return;
} else {
for (President c : cards) {
if (c.isFlipped() == true && c.isMatched() == false && c != c2) {
c1 = c;
}
}
if (c1.getPresident() == c2.getPresident()) {
cards.get(c1.getCard() - 1).setMatched(true);
cards.get(c2.getCard() - 1).setMatched(true);
} else {
cards.get(c1.getCard() - 1).setFlipped(false);
cards.get(c2.getCard() - 1).setFlipped(false);
thread = new Thread() {
@Override
public void run() {
try {
synchronized (this) {
wait(3000);
}
} catch (InterruptedException ex) {
}
// TODO
//THESE TWO LINES ARE THE ONES I WANT TO RUN AFTER 3 SECONDS
c1.getImg().setImageResource(R.drawable.facedown);
c2.getImg().setImageResource(R.drawable.facedown);
}
};
thread.start();
}
}
}
@Override
public boolean onTouchEvent(MotionEvent evt) {
if (evt.getAction() == MotionEvent.ACTION_DOWN) {
synchronized (thread) {
thread.notifyAll();
}
}
return true;
}
09-07 17:04:19.937: E/AndroidRuntime(6822): FATAL EXCEPTION: Thread-4693
09-07 17:04:19.937: E/AndroidRuntime(6822): android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
09-07 17:04:19.937: E/AndroidRuntime(6822): at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:4746)
09-07 17:04:19.937: E/AndroidRuntime(6822): at android.view.ViewRootImpl.requestLayout(ViewRootImpl.java:823)
09-07 17:04:19.937: E/AndroidRuntime(6822): at android.view.View.requestLayout(View.java:15473)
09-07 17:04:19.937: E/AndroidRuntime(6822): at android.view.View.requestLayout(View.java:15473)
09-07 17:04:19.937: E/AndroidRuntime(6822): at android.view.View.requestLayout(View.java:15473)
09-07 17:04:19.937: E/AndroidRuntime(6822): at android.view.View.requestLayout(View.java:15473)
09-07 17:04:19.937: E/AndroidRuntime(6822): at android.widget.RelativeLayout.requestLayout(RelativeLayout.java:318)
09-07 17:04:19.937: E/AndroidRuntime(6822): at android.view.View.requestLayout(View.java:15473)
09-07 17:04:19.937: E/AndroidRuntime(6822): at android.widget.ImageView.setImageResource(ImageView.java:352)
答案 0 :(得分:1)
将c1
和c2
声明为全局变量。
private Thread thread;
President c1, c2;
public void checkMatch(int num) {
flips++;
c1 = null;
c2 = cards.get(num - 1);
....
....
}
修改强>
您正在尝试从非UI线程更改UI。而不是以下:
thread = new Thread() {
@Override
public void run() {
try {
synchronized (this) {
wait(3000);
}
} catch (InterruptedException ex) {
}
// TODO
//THESE TWO LINES ARE THE ONES I WANT TO RUN AFTER 3 SECONDS
c1.getImg().setImageResource(R.drawable.facedown);
c2.getImg().setImageResource(R.drawable.facedown);
}
};
thread.start();
使用处理程序:
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
c1.getImg().setImageResource(R.drawable.facedown);
c2.getImg().setImageResource(R.drawable.facedown);
}
}, 3000L);