0

I created an android app that tests your reflects speed.

at first the app is still when you press(State 0) the app starts a thread that waits a couple of seconds before showing a green screen. When the green screen is shown(State 1) you have to press as fast as you can. if you press the green(state 2) you are shown the reflect time

the problem I'm having that when a person clicks too soon, i have to stop the thread which causes the app to crash(thread.stop()). Any suggestions?

public class MainActivity extends Activity implements OnClickListener{
    TextView tvs,tvas;
    RelativeLayout rl;
    int state; //0=red     1=green         2=tapped-green      
    long end,start,result;
    Thread th;
    Boolean dont;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        rl = (RelativeLayout) findViewById(R.id.rl);
        tvs = (TextView) findViewById(R.id.tvs);
        tvas = (TextView) findViewById(R.id.tvas);
        dont=true;

        state=0;
        Typeface myTypeface = Typeface.createFromAsset(this.getAssets(),"RobotoCondensed-Bold.ttf");
        tvs.setTypeface(myTypeface);
        tvas.setTypeface(myTypeface);
        rl.setOnClickListener(this);
        tvs.setOnClickListener(this);
        tvas.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (state) {
        case 0:
            tvs.setText("Any Min Now");
            rl.setBackgroundColor(Color.rgb(255, 68, 68));
            final Handler mHandler = new Handler();
            state=1;
            dont=true;
            th = new Thread((new Runnable() {public void run() {while (true){try {Thread.sleep(randInt());mHandler.post(new Runnable() {public void run() {
                if(dont){ //not helping
                rl.setBackgroundColor(Color.GREEN);
                start = SystemClock.uptimeMillis();
                state=2;
                }
            }});} catch (Exception e) {}}}}));
             th.start();
            break;
        case 1:
            tvs.setText("Too Soon!");
            tvas.setText("Tap AnyWhere To restart");
            dont=false;
            rl.setBackgroundColor(Color.rgb(255, 68, 68));
            state=0;
            break;
        case 2:
            end = SystemClock.uptimeMillis();
            result=end-start;
            tvs.setText("UR Reaction Time= "+result+"ms");
            tvas.setText("Tap AnyWhere To restart");
            state=0;
            dont=false;
            th.interrupt();  //giving me wrong numbers
            break;
        }

    }

    public static int randInt() {  //creates a random number
        Random rand = new Random();
        int randomNum = rand.nextInt((6000 - 2000) + 1) + 2000;
        return randomNum;
    }
}

LogCat:

03-07 22:16:05.575: I/WindowState(840): WIN DEATH: Window{45b08280 u0                                 com.afm.reflects/com.afm.reflects.MainActivity}
03-07 22:16:05.575: I/ActivityManager(840): Process com.afm.reflects (pid         32055) (adj 11) has died.
03-07 22:16:52.391: V/SmartFaceService - 3rd party pause(840): onReceive 

[android.intent.action.ACTIVITY_STATE/com.afm.reflects/create]
03-07 22:17:03.471: E/AndroidRuntime(864): Process: com.afm.reflects, PID:         864
03-07 22:17:03.471: E/AndroidRuntime(864):  at     com.afm.reflects.MainActivity.onClick(MainActivity.java:73)
03-07 22:17:03.482: W/ActivityManager(840):   Force finishing activity     com.afm.reflects/.MainActivity
03-07 22:17:03.522: V/SmartFaceService - 3rd party pause(840): onReceive 

[android.intent.action.ACTIVITY_STATE/com.afm.reflects/pause]
03-07 22:17:03.562: D/CrashAnrDetector(840): processName: com.afm.reflects
03-07 22:17:03.562: D/CrashAnrDetector(840): broadcastEvent :     com.afm.reflects data_app_crash
03-07 22:17:05.634: I/ActivityManager(840): Process com.afm.reflects (pid     864) (adj 9) has died.
03-07 22:17:05.644: I/WindowState(840): WIN DEATH: Window{45514be0 u0     com.afm.reflects/com.afm.reflects.MainActivity}
03-07 22:18:53.869: V/SmartFaceService - 3rd party pause(840): onReceive 

[android.intent.action.ACTIVITY_STATE/com.afm.reflects/create]
03-07 22:19:11.867: E/AndroidRuntime(1649): Process: com.afm.reflects, PID:     1649
03-07 22:19:11.867: E/AndroidRuntime(1649):     at     com.afm.reflects.MainActivity.onClick(MainActivity.java:73)
03-07 22:19:11.877: W/ActivityManager(840):   Force finishing activity com.afm.reflects/.MainActivity
03-07 22:19:11.917: D/CrashAnrDetector(840): processName: com.afm.reflects
03-07 22:19:11.917: V/SmartFaceService - 3rd party pause(840): onReceive 

[android.intent.action.ACTIVITY_STATE/com.afm.reflects/pause]
03-07 22:19:11.927: D/CrashAnrDetector(840): broadcastEvent : com.afm.reflects     data_app_crash
the sof asker
  • 59
  • 1
  • 6

0 Answers0