gpt4 book ai didi

android - 调用 wait() 时出现异常

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:54:48 25 4
gpt4 key购买 nike

我正在实现一个播放两种声音的应用程序(“touchandshow”,然后是“tiger”)。这是在我的 looper 方法中完成的。我第一次调用它,然后调用wait(),然后再次调用looper

问题是我在 LogCat 中从 wait() 调用中得到一个异常。

这是我的代码:

        mPlayer = MediaPlayer.create(this, R.raw.touchandshow);
mPlayer2 = MediaPlayer.create(this, R.raw.tiger);

try {

looper();
wait(2000);

looper();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

public void looper() {
CountDownTimer aCounter;
aCounter = new CountDownTimer(2000, 1000) {
public void onTick(long millisUntilFinished) {
mPlayer.start();
}

public void onFinish() {
mPlayer2.start();
}
};
aCounter.start();

}

还有 LogCat:

    07-29 10:20:09.412: ERROR/AndroidRuntime(1188): FATAL EXCEPTION: main
07-29 10:20:09.412: ERROR/AndroidRuntime(1188): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.AudioTesting/com.AudioTesting.AudioTesting}: java.lang.IllegalMonitorStateException: object not locked by thread before wait()
07-29 10:20:09.412: ERROR/AndroidRuntime(1188): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
07-29 10:20:09.412: ERROR/AndroidRuntime(1188): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
07-29 10:20:09.412: ERROR/AndroidRuntime(1188): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
07-29 10:20:09.412: ERROR/AndroidRuntime(1188): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
07-29 10:20:09.412: ERROR/AndroidRuntime(1188): at android.os.Handler.dispatchMessage(Handler.java:99)
07-29 10:20:09.412: ERROR/AndroidRuntime(1188): at android.os.Looper.loop(Looper.java:123)
07-29 10:20:09.412: ERROR/AndroidRuntime(1188): at android.app.ActivityThread.main(ActivityThread.java:4627)
07-29 10:20:09.412: ERROR/AndroidRuntime(1188): at java.lang.reflect.Method.invokeNative(Native Method)
07-29 10:20:09.412: ERROR/AndroidRuntime(1188): at java.lang.reflect.Method.invoke(Method.java:521)
07-29 10:20:09.412: ERROR/AndroidRuntime(1188): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
07-29 10:20:09.412: ERROR/AndroidRuntime(1188): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
07-29 10:20:09.412: ERROR/AndroidRuntime(1188): at dalvik.system.NativeStart.main(Native Method)
07-29 10:20:09.412: ERROR/AndroidRuntime(1188): Caused by: java.lang.IllegalMonitorStateException: object not locked by thread before wait()
07-29 10:20:09.412: ERROR/AndroidRuntime(1188): at java.lang.Object.wait(Native Method)
07-29 10:20:09.412: ERROR/AndroidRuntime(1188): at java.lang.Object.wait(Object.java:326)
07-29 10:20:09.412: ERROR/AndroidRuntime(1188): at com.AudioTesting.AudioTesting.onCreate(AudioTesting.java:25)
07-29 10:20:09.412: ERROR/AndroidRuntime(1188): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
07-29 10:20:09.412: ERROR/AndroidRuntime(1188): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
07-29 10:20:09.412: ERROR/AndroidRuntime(1188): ... 11 more

谁能告诉我我做错了什么?

最佳答案

java.lang.IllegalMonitorStateException: object not locked by thread before wait()

您的问题是这条消息。你不能在你没有先锁定的对象上调用 waitwait 用于同步,而不是通用的“ sleep ”工具。

如果你只是想暂停你的线程,使用:

Thread.sleep(2000);

改为在您的 try block 中。

关于android - 调用 wait() 时出现异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6868884/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com