我有一个 frameAnimation,我只希望它启动 3 秒左右......因此我在一个线程中添加了等待 3 秒,但后来我遇到了一个异常。
我之前没有用线程做过任何工作,所以这几乎是我第一次使用它,有人可以帮忙吗?
这是我的代码:
public void setTimerImage4Bot() throws Exception {
Thread thread = new Thread();
AnimationDrawable frameAnimation = (AnimationDrawable)bot.getDrawable();
frameAnimation.setCallback(bot);
frameAnimation.setVisible(true, true);
frameAnimation.start();
Log.i("BaoAnh","START");
thread.wait(3000);
frameAnimation.stop();
Log.i("BaoAnh","STOP");
generateRandom();
}
这是错误:
05-21 10:11:55.633: WARN/System.err(740): java.lang.IllegalMonitorStateException: object not locked by thread before wait()
05-21 10:11:55.633: WARN/System.err(740): at java.lang.Object.wait(Native Method)
05-21 10:11:55.633: WARN/System.err(740): at java.lang.Object.wait(Object.java:326)
05-21 10:11:55.633: WARN/System.err(740): at com.example.PaperCissorsRock.game.setTimerImage4Bot(game.java:63)
05-21 10:11:55.633: WARN/System.err(740): at com.example.PaperCissorsRock.game$1.onClick(game.java:44)
05-21 10:11:55.633: WARN/System.err(740): at android.view.View.performClick(View.java:2408)
05-21 10:11:55.633: WARN/System.err(740): at android.view.View$PerformClick.run(View.java:8816)
05-21 10:11:55.633: WARN/System.err(740): at android.os.Handler.handleCallback(Handler.java:587)
05-21 10:11:55.633: WARN/System.err(740): at android.os.Handler.dispatchMessage(Handler.java:92)
05-21 10:11:55.633: WARN/System.err(740): at android.os.Looper.loop(Looper.java:123)
05-21 10:11:55.633: WARN/System.err(740): at android.app.ActivityThread.main(ActivityThread.java:4627)
05-21 10:11:55.643: WARN/System.err(740): at java.lang.reflect.Method.invokeNative(Native Method)
05-21 10:11:55.643: WARN/System.err(740): at java.lang.reflect.Method.invoke(Method.java:521)
05-21 10:11:55.643: WARN/System.err(740): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
05-21 10:11:55.643: WARN/System.err(740): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
05-21 10:11:55.643: WARN/System.err(740): at dalvik.system.NativeStart.main(Native Method)