Skip to content

Snappy1

  • Home
  • Android
  • What
  • How
  • Is
  • Can
  • Does
  • Do
  • Why
  • Are
  • Who
  • Toggle search form

[FIXED] java – New coder, doing third project, can not get app to run

Posted on November 11, 2022 By

Solution 1 :

  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(R.id.btnGuess);

Problem :

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.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
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!";
               newGame();
           }
       } catch (Exception e) {
           message = "Enter a whole number between 1 and 100.";
       } finally {
           lblOutput.setText(message);
           txtGuess.requestFocus();
           txtGuess.selectAll();
       }

   }


   private AppBarConfiguration appBarConfiguration;

   public void newGame() {
       theNumber = (int)(Math.random() * 100 + 1);
   }
   @Override
   protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_main);

       txtGuess = findViewById(R.id.txtGuess);
       Button btnGuess = findViewById(R.id.btnGuess);
       lblOutput = findViewById (R.id.lblOutput);
       newGame();
       btnGuess.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick (View v) {
               checkGuess();
           }
       });


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

       setSupportActionBar(binding.toolbar);

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

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

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

   @Override
   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 == R.id.action_settings) {
           return true;
       }

       return super.onOptionsItemSelected(item);
   }

   @Override
   public boolean onSupportNavigateUp() {
       NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment_content_main);
       return NavigationUI.navigateUp(navController, appBarConfiguration)
               || super.onSupportNavigateUp();
   }
}

Comments

Comment posted by Tyler V

You are using both view binding and manual setContentView, this

READ  [FIXED] android - How can I implement RefreshIndicator in Listview.Builder using Getx?
Powered by Inline Related Posts

Comment posted by Billy Hodge

Can you explain a little better? I am not understanding, or where you are referencing.

Comment posted by Tyler V

You currently call

Android Tags:android, java

Post navigation

Previous Post: [FIXED] Android ACTION_VIEW intent is not opening urls in non-browser apps like twitter and instagram
Next Post: [FIXED] android – Check if activity is active, and if active pass data from service to activity?

Related Posts

[FIXED] android – Flutter – Reorderable List with a Listview nested inside expansiontile Android
[FIXED] How to play videos in Android Studio Android
[FIXED] android – How to implement SearchView on fragment that is already fetching arrayList from firestore KOTLIN Android
[FIXED] gson – Dynamic JSON parsing in android Retrofit? Android
[FIXED] android – Uploaded an Application to the play store but it won’t open Android
[FIXED] android – Animations doesn’t work in debug mode | React-Native Android

Archives

  • March 2023
  • February 2023
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022

Categories

  • ¿Cómo
  • ¿Cuál
  • ¿Cuándo
  • ¿Cuántas
  • ¿Cuánto
  • ¿Qué
  • Android
  • Are
  • At
  • C'est
  • Can
  • Comment
  • Did
  • Do
  • Does
  • Est-ce
  • Est-il
  • For
  • Has
  • Hat
  • How
  • In
  • Is
  • Ist
  • Kann
  • Où
  • Pourquoi
  • Quand
  • Quel
  • Quelle
  • Quelles
  • Quels
  • Qui
  • Should
  • Sind
  • Sollte
  • Uncategorized
  • Wann
  • Warum
  • Was
  • Welche
  • Welchen
  • Welcher
  • Welches
  • Were
  • What
  • What's
  • When
  • Where
  • Which
  • Who
  • Who's
  • Why
  • Wie
  • Will
  • Wird
  • Wo
  • Woher
  • you can create a selvedge edge: You can make the edges of garter stitch more smooth by slipping the first stitch of every row.2022-02-04
  • you really only need to know two patterns: garter stitch

Recent Posts

  • What is the rising action in Julius Caesar?
  • How do you secure a rope to itself?
  • Does waterproof laminate scratch easily?
  • What makes a building prewar?
  • What can you learn in a month without alcohol?

Recent Comments

No comments to show.

Copyright © 2023 Snappy1.

Powered by PressBook Grid Dark theme