In my project, I has a layout as follows:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/
android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical">

    <Button
        android:id="@+id/startButton"
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:text="start"
         />
    <Button
        android:id="@+id/stopButton"
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:text="stop"
         />
</LinearLayout>
It is quite simple. There are two buttons. One is start and the other
is stop.

And an Activity as follows:
package button.test;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

public class MainActivity extends Activity {
        private static final String TAG = "MainActivity";
        private Button startButton;
        private Button stopButton;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        startButton = (Button)findViewById(R.id.startButton);
        startButton.setOnClickListener(new View.OnClickListener() {

                        @Override
                        public void onClick(View arg0) {
                                Log.d(TAG,"startButton isEnabled(): 
"+arg0.isEnabled()+" executing
on thread "+ Thread.currentThread().getName());
                                if(arg0.isEnabled()==false){
                                        throw new 
RuntimeException("startButton");
                                }
                                arg0.setEnabled(false);
                                stopButton.setEnabled(true);
                        }
                });
        stopButton = (Button)findViewById(R.id.stopButton);
        stopButton.setOnClickListener(new View.OnClickListener() {

                        @Override
                        public void onClick(View arg0) {
                                Log.d(TAG,"stopButton isEnabled(): 
"+arg0.isEnabled()+" executing
on thread "+ Thread.currentThread().getName());
                                if(arg0.isEnabled()==false){
                                        throw new 
RuntimeException("stopButton");
                                }
                                arg0.setEnabled(false);
                                startButton.setEnabled(true);
                        }
                });
    }
}
The logic is: (1)When the start button is clicked, disable it and
enable the stop button. (2)When the stop button is clicked, disable it
and enable the start button.

What I expect is that when the onClickListener of a button is
executing, the button state should be enabled. It is impossible to
fire the onClickListener when the button is disabled. Therefore, I add
the if block and the RuntimeException to detect it.

It works when I interact with it but it crashes when I run the monkey
test (adb shell monkey -p button.test -v 50000).

The logcat messages are as follows:
D/MainActivity(1836): startButton isEnabled(): true main
D/MainActivity(1836): stopButton isEnabled(): true main
D/MainActivity(1836): startButton isEnabled(): true main
D/MainActivity(1836): startButton isEnabled(): false main
D/AndroidRuntime(1836): Shutting down VM
W/dalvikvm(1836): threadid=1: thread exiting with uncaught exception
(group=0x41745300)
E/AndroidRuntime(1836): FATAL EXCEPTION: main
E/AndroidRuntime(1836): java.lang.RuntimeException: startButton
E/AndroidRuntime(1836):         at button.test.MainActivity
$1.onClick(MainActivity.java:24)
E/AndroidRuntime(1836):         at android.view.View.performClick(View.java:
4084)
E/AndroidRuntime(1836):         at android.view.View
$PerformClick.run(View.java:16966)
E/AndroidRuntime(1836):         at
android.os.Handler.handleCallback(Handler.java:615)
E/AndroidRuntime(1836):         at
android.os.Handler.dispatchMessage(Handler.java:92)
E/AndroidRuntime(1836):         at android.os.Looper.loop(Looper.java:137)
E/AndroidRuntime(1836):         at
android.app.ActivityThread.main(ActivityThread.java:4745)
E/AndroidRuntime(1836):         at
java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(1836):         at
java.lang.reflect.Method.invoke(Method.java:511)
E/AndroidRuntime(1836):         at com.android.internal.os.ZygoteInit
$MethodAndArgsCaller.run(ZygoteInit.java:786)
E/AndroidRuntime(1836):         at
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
E/AndroidRuntime(1836):         at dalvik.system.NativeStart.main(Native
Method)

Note that at line 4, the button state is disabled when the
onClickListener is executing!!! It is quite strange. In addition, both
the onClickListeners are run on the main thread.
What I expect is that there is no race condition between the
onClickListeners because they run on the same thread.

Can anyone explain why the program crashes when I run the monkey
test?

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to