在调用异常时,等待() [英] Exception when calling wait()
问题描述
我在实现我打两个声音的应用程序( touchandshow ,其次是虎)。这是在我的尺蠖
方法完成。我把它称为第一次,然后调用的wait()
,然后调用尺蠖
试。
问题是,我从LogCat中得到一个异常的等待()
电话。
下面是我的code:
= MPLAYER MediaPlayer.create(这一点,R.raw.touchandshow);
mPlayer2 = MediaPlayer.create(这一点,R.raw.tiger); 尝试{ 活套();
等待(2000年); 活套();
}赶上(InterruptedException的E){
// TODO自动生成catch块
e.printStackTrace();
}}公共无效尺蠖(){
CountDownTimer aCounter;
aCounter =新CountDownTimer(2000,1000){
公共无效onTick(长millisUntilFinished){
mPlayer.start();
} 公共无效onFinish(){
mPlayer2.start();
}
};
aCounter.start();}
而LogCat中:
10月7日至29日:20:09.412:ERROR / AndroidRuntime(1188):致命异常:主要
10月7日至29日:20:09.412:ERROR / AndroidRuntime(1188):了java.lang.RuntimeException:无法启动活动ComponentInfo {com.AudioTesting / com.AudioTesting.AudioTesting}:java.lang.IllegalMonitorStateException:对象不是线程锁定等待前()
10月7日至29日:20:09.412:ERROR / AndroidRuntime(1188):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
10月7日至29日:20:09.412:ERROR / AndroidRuntime(1188):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
10月7日至29日:20:09.412:ERROR / AndroidRuntime(1188):在android.app.ActivityThread.access $ 2300(ActivityThread.java:125)
10月7日至29日:20:09.412:ERROR / AndroidRuntime(1188):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:2033)
10月7日至29日:20:09.412:ERROR / AndroidRuntime(1188):在android.os.Handler.dispatchMessage(Handler.java:99)
10月7日至29日:20:09.412:ERROR / AndroidRuntime(1188):在android.os.Looper.loop(Looper.java:123)
10月7日至29日:20:09.412:ERROR / AndroidRuntime(1188):在android.app.ActivityThread.main(ActivityThread.java:4627)
10月7日至29日:20:09.412:ERROR / AndroidRuntime(1188):在java.lang.reflect.Method.invokeNative(本机方法)
10月7日至29日:20:09.412:ERROR / AndroidRuntime(1188):在java.lang.reflect.Method.invoke(Method.java:521)
10月7日至29日:20:09.412:ERROR / AndroidRuntime(1188):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:868)
10月7日至29日:20:09.412:ERROR / AndroidRuntime(1188):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
10月7日至29日:20:09.412:ERROR / AndroidRuntime(1188):在dalvik.system.NativeStart.main(本机方法)
10月7日至29日:20:09.412:ERROR / AndroidRuntime(1188):java.lang.IllegalMonitorStateException:所造成的等待线程对象之前未锁定()
10月7日至29日:20:09.412:ERROR / AndroidRuntime(1188):在java.lang.Object.wait(本机方法)
10月7日至29日:20:09.412:ERROR / AndroidRuntime(1188):在java.lang.Object.wait(Object.java:326)
10月7日至29日:20:09.412:ERROR / AndroidRuntime(1188):在com.AudioTesting.AudioTesting.onCreate(AudioTesting.java:25)
10月7日至29日:20:09.412:ERROR / AndroidRuntime(1188):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
10月7日至29日:20:09.412:ERROR / AndroidRuntime(1188):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
10月7日至29日:20:09.412:ERROR / AndroidRuntime(1188):11 ...更多
谁能告诉我什么,我做错了吗?
java.lang.IllegalMonitorStateException:由线程等待对象之前未锁定()
您的问题是此消息。你不能叫等
你有没有先锁定的对象。 等
是为同步的,而不是作为一种通用的休眠功能。
如果你只是想暂停线程,使用方法:
视频下载(2000年);
您try块内,而不是
I am implementing an application in which I play two sounds ("touchandshow" followed by "tiger"). This is done in my looper
method. I call it a first time, then call wait()
, then call looper
again.
The problem is that I get an exception in the LogCat from the wait()
call.
Here is my code:
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();
}
And the 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
Can anyone tell me what I'm doing wrong?
java.lang.IllegalMonitorStateException: object not locked by thread before wait()
Your problem is this message. You can't call wait
on an object you haven't locked first. wait
is meant for synchronization, not as a generic "sleep" facility.
If you just want to pause your thread, use:
Thread.sleep(2000);
inside your try block instead.
这篇关于在调用异常时,等待()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!