• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

[SOLVED]Random Guess Game App Crashes

Hey guys, complete newbie here!

Essentially, I'm creating a game where the player has to guess the randomly generated number. The Program compiles, however after I enter a number and click the button to enter the App crashes. Any help + general tips are welcome. Thank you!

main activity code:
Java:
package com.example.week_1_java;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {
    //elements we are intercating iwth
    Guess game;
    TextView textView;  //INSTRUCTIONS
    TextView textView2;
    EditText edittext2; // TEXT FIELD FOR USER INPUT OF NUMBER GUESS
    Button button1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //getting the ui elements

        textView = findViewById(R.id.textView);
        textView2 = findViewById(R.id.textView2);
        edittext2 = findViewById(R.id.editText2);
        button1 = findViewById(R.id.button1);

        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
game.setNewValue();
                makeguess1();

            }
        });


    }

public void makeguess1(){



        //Get our Guess value from the edit text by getting the input text and
        //casting the number from the string
    String usertext = edittext2.getText().toString();
    int result = Integer.parseInt(usertext);


        //remove the text from the edit text so that the user knows that the guess has
        //been input
        edittext2.setText("");

            //pass the guess to the Guess object and change the result text
            //and button state based on the result
            if (game.MakeGuess(result)) {

                //user was correct so set the result text to the win condition
                textView2.setText(R.string.correct);
                //set the button state to try again so user can reset the game


            } else {

                //otherwise user was incorrect so set the result text
                textView2.setText(R.string.incorrect);

            }
        }
}

My Guess Class Code:
Java:
package com.example.week_1_java;
import java.util.Random;

public class Guess {
    int valueToGuess;
    Random randomguess = new Random();
    int Range =15;
    Guess()
    {
        setNewValue();


    };
    void setNewValue()
    {
        valueToGuess = 1+randomguess.nextInt(Range-1);

    };


    public boolean MakeGuess(int userToGuess)
    {
        boolean correct = false;
        if(userToGuess == valueToGuess){
            correct = true;
        }


        return correct;
    };
    }
 

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones