Coding Horror, be scared

Simple Android App

The app crashes after switching to this activity. It isn't a problem with the manifest or any of that because it worked fine before I actually filled in the class with real code.

It is supposed to be a game that switches screen colors between red, yellow, and green. The player is awarded 2 points for tapping it when it is yellow, one for when it is green, and they lose if they tap on a red screen. Over time, the changes become shorter and less predictable. In the onCreate() method, there is an intro that plays a "Ready, set, go!" type of 'animation'. The previous activity was just the splash screen.

package com.nateolson.taptap; import; import android.os.Handler; import; import android.os.Bundle; import android.view.MotionEvent; import android.view.View; import android.widget.TextView; import java.util.concurrent.ThreadLocalRandom; public class GameActivity extends AppCompatActivity { final Handler introHandler = new Handler(); boolean gameActive = false; final View layout = findViewById(; //for main method only int playerPoints = 0; long timeMinMillis = 1000; long timeMaxMillis = 3000; long randTime; long switchReferenceTime = System.currentTimeMillis(); String colorStage = "green"; //for main method only @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_game); final TextView readyMessageView = (TextView) findViewById(; final TextView getSetMessageView = (TextView) findViewById(; getSetMessageView.setVisibility(View.INVISIBLE); final TextView goMessageView = (TextView) findViewById(; goMessageView.setVisibility(View.INVISIBLE); layout.setBackgroundColor(Color.RED); Runnable switchToYellow = new Runnable() { @Override public void run() { readyMessageView.setVisibility(View.GONE); layout.setBackgroundColor(Color.YELLOW); getSetMessageView.setVisibility(View.VISIBLE); } }; Runnable switchToGreen = new Runnable() { @Override public void run() { getSetMessageView.setVisibility(View.GONE); layout.setBackgroundColor(Color.GREEN); goMessageView.setVisibility(View.VISIBLE); } }; Runnable gameRunnable = new Runnable() { public void run() { gameActive = true; } }; introHandler.postDelayed(switchToYellow, 1500); introHandler.postDelayed(switchToGreen, 3000); introHandler.postDelayed(gameRunnable, 4500); main(); } public void main() { while(gameActive) { randTime = ThreadLocalRandom.current().nextLong(timeMinMillis, timeMaxMillis); if (System.currentTimeMillis() - switchReferenceTime >= randTime) { switch (colorStage) { case "green": colorStage = "yellow"; break; case "yellow": colorStage = "red"; break; case "red": colorStage = "green"; break; } switchReferenceTime = System.currentTimeMillis(); } switch (colorStage) { case "green": layout.setBackgroundColor(Color.GREEN); case "yellow": layout.setBackgroundColor(Color.YELLOW); case "red": layout.setBackgroundColor(Color.RED); } timeMinMillis -= .03 * timeMinMillis; timeMaxMillis -= .02 * timeMaxMillis; layout.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { switch (colorStage) { case "green": playerPoints++; case "yellow": playerPoints += 2; case "red": playerPoints = 0; gameActive = false; } return false; } }); } } }

Reactions 0 reactions

No reactions yet, be the first: comment or tweak the code

Comment on this code:

Please Login or Register to leave a comment.