简体   繁体   中英

OnClickListener and Toast not cooperating

I've been working on this program for an Android app assignment, and though Eclipse has no problems with the code, my phone can't seem to run it. I am a novice in Android programming, so please bear with me.

This Android app is a simple "Guess My Number" game in a blank activity. The user is to guess from 1-100, enter their answer inside a EditText view, and submit it with a push of a button. The design is fine, but getting it to work with OnClickListener is a hassle. The app crashes on my GS3 as soon as I press the button. The most troubling part is getting the button to act and give outputs in the form of Toast.

Attached is the code from MainActivity.java.

I managed to pick up different snippets of code through StackOverflow as well as a bit of Java that I knew. The result is imperfect; it was worth trying though.

You may see my complete project here . Thank you for your time, and I appreciate whatever help I can get.

package com.lookjohn.guessnumber;

import java.util.Random;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity {
Random random; 
Button button;
EditText text;

int input; 
int MIN = 1, MAX = 100;
int comp;
int guesses;

public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    random = new Random();
    button = (Button)findViewById(R.id.button1);
    text = (EditText)findViewById(R.id.editText1);
    comp = random.nextInt(MAX - MIN + 1) + MIN;
    guesses = 0;

    button.setOnClickListener(myhandler1);
}

View.OnClickListener myhandler1 = new View.OnClickListener() {

    public void onClick(View v) {
        String value = text.getText().toString(); // Get value from input from editTextView
        input = Integer.parseInt(value); // Turn string into integer

        do{
            guesses++;
            if(input > comp)
                Toast.makeText(MainActivity.this, 
                    "Number is too big.", 
                    Toast.LENGTH_SHORT).show();
            else if (input < comp)
                Toast.makeText(MainActivity.this, 
                    "Number is too small.", 
                    Toast.LENGTH_SHORT).show();
            else
                Toast.makeText(MainActivity.this, 
                    "Good job! That was correct." +
                    "You made " + guesses + " guesses.", 
                    Toast.LENGTH_SHORT).show();
        } while(input != comp);

    }
};

Edit: I've found a couple other issues in your code. First, your EditText's input type is phone. It should be number. Also, a user can submit a blank edit text, which will cause the app to crash when you call parseInt . I've taken care of that in some changes to your code below.


The problem is in your loop. After submit is pressed, you are entering an infinite loop. I think you are expecting the user to be able to submit multiple guesses. But if the user has entered "3" and your computed value is "10", all your loop is doing is determining that 3 != 10 over and over and over again. This causes the UI to freeze.

Removing your while loop will allow the Toast to show:

public void onClick(View v) {

        // If you want to implement a max number of guesses, detect the 
        // number of guesses and return from the method.
        if (guesses > 5) {
            Toast.makeText(MainActivity.this, "Out of guesses!", Toast.LENGTH_SHORT);
            return;
        }

        String value = text.getText().toString(); // Get value from input from editTextView
        // If the user submits an empty EditText, return so we don't crash when parsing int
        if (value.isEmpty()) {
            Toast.makeText(MainActivity.this, "You must enter a guess!", Toast.LENGTH_SHORT);
            return;
        }
        input = Integer.parseInt(value); // Turn string into integer
        guesses++;
        if(input > comp)
            Toast.makeText(MainActivity.this, 
                "Number is too big.", 
                Toast.LENGTH_SHORT).show();
        else if (input < comp)
            Toast.makeText(MainActivity.this, 
                "Number is too small.", 
                Toast.LENGTH_SHORT).show();
        else
            Toast.makeText(MainActivity.this, 
                "Good job! That was correct." +
                "You made " + guesses + " guesses.", 
                Toast.LENGTH_SHORT).show();

    }

And in your main.xml, change your inputType from phone to number :

<EditText
    android:id="@+id/editText1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/button1"
    android:layout_alignRight="@+id/button1"
    android:layout_centerVertical="true"
    android:ems="10"
    android:inputType="number" />

You have made the infinite loop in your click listener for the guess variable which is always getting incremented and doesn't stops.

Just remove the do{...}while loop and then try out. As there doesn't seems to require any use of it.

        if(input > comp)
            Toast.makeText(MainActivity.this, 
                "Number is too big.", 
                Toast.LENGTH_SHORT).show();
        else if (input < comp)
            Toast.makeText(MainActivity.this, 
                "Number is too small.", 
                Toast.LENGTH_SHORT).show();
        else
            Toast.makeText(MainActivity.this, 
                "Good job! That was correct." +
                "You made " + guesses + " guesses.", 
                Toast.LENGTH_SHORT).show();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM