我有一个变量,当我单击一个按钮时,它会更改(设置为 1),然后应该输入一段时间,因为变量设置为 1 但它没有。在 logcat Metronome 中,单击的消息被打印,但 InWhile 消息没有被打印。为什么?我应该怎么做才能进去?
metronomepp.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if (ms==0) {ms=1;}
Log.i("Metronome clicked", ""+ms);
}
});
while (ms == 1) {
Log.i("Metronome", "InWhile");
if (metronome.isPlaying()) {metronome.stop();}
metronome.seekTo(0);
metronome.setOnSeekCompleteListener(null);
metronome.start();
metronome.setOnCompletionListener(null);
metronomepp.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
ms=0;
}
});
}
已解决:在点击时移动!但是现在应用程序崩溃了。
按下按钮后的 LogCat(声音只播放一次,然后应用程序崩溃):
03-14 19:10:30.584: I/Metronome clicked(25448): 1
03-14 19:10:30.584: I/Metronome(25448): InWhile
03-14 19:10:30.584: V/MediaPlayer(25448): isPlaying: 0
03-14 19:10:30.584: V/MediaPlayer-JNI(25448): isPlaying: 0
03-14 19:10:30.584: V/MediaPlayer-JNI(25448): seekTo: 0(msec)
03-14 19:10:30.584: V/MediaPlayer(25448): seekTo 0
03-14 19:10:30.584: V/MediaPlayer(25448): getDuration
03-14 19:10:30.584: V/MediaPlayer(25448): message received msg=4, ext1=0, ext2=0
03-14 19:10:30.589: V/MediaPlayer(25448): Received seek complete
03-14 19:10:30.589: V/MediaPlayer(25448): All seeks complete - return to regularly scheduled program
03-14 19:10:30.589: V/MediaPlayer(25448): callback application
03-14 19:10:30.589: V/MediaPlayer(25448): back from callback
03-14 19:10:30.589: V/MediaPlayer-JNI(25448): start
03-14 19:10:30.589: V/MediaPlayer(25448): start
03-14 19:10:30.619: D/AndroidRuntime(25448): Shutting down VM
03-14 19:10:30.619: W/dalvikvm(25448): threadid=1: thread exiting with uncaught exception (group=0x40c581f8)
03-14 19:10:30.624: E/AndroidRuntime(25448): FATAL EXCEPTION: main
03-14 19:10:30.624: E/AndroidRuntime(25448): java.lang.IllegalMonitorStateException: object not locked by thread before wait()
03-14 19:10:30.624: E/AndroidRuntime(25448): at java.lang.Object.wait(Native Method)
03-14 19:10:30.624: E/AndroidRuntime(25448): at java.lang.Object.wait(Object.java:401)
03-14 19:10:30.624: E/AndroidRuntime(25448): at com.example.guitartools.MetronomeActivity$3.onClick(MetronomeActivity.java:72)
03-14 19:10:30.624: E/AndroidRuntime(25448): at android.view.View.performClick(View.java:3627)
03-14 19:10:30.624: E/AndroidRuntime(25448): at android.view.View$PerformClick.run(View.java:14329)
03-14 19:10:30.624: E/AndroidRuntime(25448): at android.os.Handler.handleCallback(Handler.java:605)
03-14 19:10:30.624: E/AndroidRuntime(25448): at android.os.Handler.dispatchMessage(Handler.java:92)
03-14 19:10:30.624: E/AndroidRuntime(25448): at android.os.Looper.loop(Looper.java:137)
03-14 19:10:30.624: E/AndroidRuntime(25448): at android.app.ActivityThread.main(ActivityThread.java:4511)
03-14 19:10:30.624: E/AndroidRuntime(25448): at java.lang.reflect.Method.invokeNative(Native Method)
03-14 19:10:30.624: E/AndroidRuntime(25448): at java.lang.reflect.Method.invoke(Method.java:511)
03-14 19:10:30.624: E/AndroidRuntime(25448): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:980)
03-14 19:10:30.624: E/AndroidRuntime(25448): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:747)
03-14 19:10:30.624: E/AndroidRuntime(25448): at dalvik.system.NativeStart.main(Native Method)
03-14 19:10:30.969: V/MediaPlayer(25448): message received msg=2, ext1=0, ext2=0
03-14 19:10:30.969: V/MediaPlayer(25448): playback complete
03-14 19:10:30.969: V/MediaPlayer(25448): callback application
03-14 19:10:30.969: V/MediaPlayer(25448): back from callback
这是在while中使用Thread之后的代码:
metronomepp.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if (ms==0) {ms=1;}
while (ms == 1) {
runThread();
}
}
});
这是onCreate之外的线程代码:
private void runThread() {
new Thread() {
public void run() {
if (metronome.isPlaying()) {metronome.pause();}
metronome.seekTo(0);
metronome.setOnSeekCompleteListener(null);
metronome.start();
metronome.setOnCompletionListener(null);
try {
Thread.sleep((long) timetw);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
metronomepp.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Log.i("Metronome", "InWhile1");
ms=0;
}
});
}
}.start();
}
并且在启动此活动后 LogCat 输出:
03-14 19:56:46.214: D/AndroidRuntime(28668): Shutting down VM
03-14 19:56:46.214: W/dalvikvm(28668): threadid=1: thread exiting with uncaught exception (group=0x40c581f8)
03-14 19:56:46.219: E/AndroidRuntime(28668): FATAL EXCEPTION: main
03-14 19:56:46.219: E/AndroidRuntime(28668): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.crnibero.guitartools/com.example.guitartools.MetronomeActivity}: java.lang.NullPointerException
03-14 19:56:46.219: E/AndroidRuntime(28668): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1891)
03-14 19:56:46.219: E/AndroidRuntime(28668): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1992)
03-14 19:56:46.219: E/AndroidRuntime(28668): at android.app.ActivityThread.access$600(ActivityThread.java:127)
03-14 19:56:46.219: E/AndroidRuntime(28668): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1158)
03-14 19:56:46.219: E/AndroidRuntime(28668): at android.os.Handler.dispatchMessage(Handler.java:99)
03-14 19:56:46.219: E/AndroidRuntime(28668): at android.os.Looper.loop(Looper.java:137)
03-14 19:56:46.219: E/AndroidRuntime(28668): at android.app.ActivityThread.main(ActivityThread.java:4511)
03-14 19:56:46.219: E/AndroidRuntime(28668): at java.lang.reflect.Method.invokeNative(Native Method)
03-14 19:56:46.219: E/AndroidRuntime(28668): at java.lang.reflect.Method.invoke(Method.java:511)
03-14 19:56:46.219: E/AndroidRuntime(28668): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:980)
03-14 19:56:46.219: E/AndroidRuntime(28668): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:747)
03-14 19:56:46.219: E/AndroidRuntime(28668): at dalvik.system.NativeStart.main(Native Method)
03-14 19:56:46.219: E/AndroidRuntime(28668): Caused by: java.lang.NullPointerException
03-14 19:56:46.219: E/AndroidRuntime(28668): at android.app.Activity.findViewById(Activity.java:1794)
03-14 19:56:46.219: E/AndroidRuntime(28668): at com.example.guitartools.MetronomeActivity.<init>(MetronomeActivity.java:29)
03-14 19:56:46.219: E/AndroidRuntime(28668): at java.lang.Class.newInstanceImpl(Native Method)
03-14 19:56:46.219: E/AndroidRuntime(28668): at java.lang.Class.newInstance(Class.java:1319)
03-14 19:56:46.219: E/AndroidRuntime(28668): at android.app.Instrumentation.newActivity(Instrumentation.java:1026)
03-14 19:56:46.219: E/AndroidRuntime(28668): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1882)
03-14 19:56:46.219: E/AndroidRuntime(28668): ... 11 more