Android SDK虽然不想启动但条件为真

时间:2013-03-14 17:47:05

标签: android while-loop conditional-statements

我有一个变量(设置为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;
            }
        });
    }

解决:移动到onClick!但现在应用程序崩溃。

按下按钮后的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

以下是使用Thread in while时的代码:

    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();
}

在tyring之后输出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

1 个答案:

答案 0 :(得分:4)

您必须将您的While循环代码放在onClick中。