New coder, doing third project, can not get app to run


I am a new coder, creating a higher or lower guessing game app in Android Studio using the Java language. My goal for this project is for the user to be able to input a number between 1 and 100, and for the app to be able to pick a random number each time the user plays the game, and for the app to be able to tell the user if the number they picked is the correct answer, or higher or lower. I am using Android studio using Java. I have all the code there, I just keep getting a couple error answers. For instance, on my "if (guess < theNumber) statements, the word "guess is in red" and says it cannot resolve symbol guess. And the statement "private Button btnGuess" the btnGuess is in grey and it says that it’s never used, but I have the id in the design tab of Android Studio and it’s id is labeled btnGuess and I thought that I wired it into the code, when I try to run the app through the emulator it just does not even run. I’ve tried googling around for the answer, but since I am new to coding I’m not sure that I’m asking the correct questions, I’ve included the code for reference, thank you very much in advance.

ckage com.example.billyhodgesguessinggame;

import android.os.Bundle;
import android.view.View;

import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.navigation.ui.AppBarConfiguration;
import androidx.navigation.ui.NavigationUI;

import com.example.billyhodgesguessinggame.databinding.ActivityMainBinding;

import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
   private EditText txtGuess;
   private Button btnGuess;
   private TextView lblOutput;
   private int theNumber;
   public void checkGuess() {
       String guessText = txtGuess.getText().toString();
       String message = "";
       try {
           int Guess = Integer.parseInt(guessText);
           if (guess < theNumber)
               message = guess + "is too low. Try again.";
           else if (guess > theNumber)
               message = guess + "is too high. Try again.";
           else {
               message = guess +
                       "is correct. You win! Let's play again!";
       } catch (Exception e) {
           message = "Enter a whole number between 1 and 100.";
       } finally {


   private AppBarConfiguration appBarConfiguration;

   public void newGame() {
       theNumber = (int)(Math.random() * 100 + 1);
   protected void onCreate(Bundle savedInstanceState) {

       txtGuess = findViewById(;
       Button btnGuess = findViewById(;
       lblOutput = findViewById (;
       btnGuess.setOnClickListener(new View.OnClickListener() {
           public void onClick (View v) {

       com.example.billyhodgesguessinggame.databinding.ActivityMainBinding binding = ActivityMainBinding.inflate(getLayoutInflater());


       NavController navController = Navigation.findNavController(this,;
       appBarConfiguration = new AppBarConfiguration.Builder(navController.getGraph()).build();
       NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration);

       binding.fab.setOnClickListener(new View.OnClickListener() {
           public void onClick(View view) {
               Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                       .setAction("Action", null).show();

   public boolean onCreateOptionsMenu(Menu menu) {
       // Inflate the menu; this adds items to the action bar if it is present.
       getMenuInflater().inflate(, menu);
       return true;

   public boolean onOptionsItemSelected(MenuItem item) {
       // Handle action bar item clicks here. The action bar will
       // automatically handle clicks on the Home/Up button, so long
       // as you specify a parent activity in AndroidManifest.xml.
       int id = item.getItemId();

       //noinspection SimplifiableIfStatement
       if (id == {
           return true;

       return super.onOptionsItemSelected(item);

   public boolean onSupportNavigateUp() {
       NavController navController = Navigation.findNavController(this,;
       return NavigationUI.navigateUp(navController, appBarConfiguration)
               || super.onSupportNavigateUp();


  1. You use Guess and guess as variable names. Stick to the lowercase version and only use guess.

  2. In this line, remove Button at the beginning. Otherwise, you declare a new variable instead of reusing the existing one which you already declared at the top.

 Button btnGuess = findViewById(;

Answered By – Alexander Hoffmann

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published