简体   繁体   English

OnClickListener和Toast不合作

[英]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. 我一直在为Android应用程序分配该程序,尽管Eclipse的代码没有问题,但我的手机似乎无法运行它。 I am a novice in Android programming, so please bear with me. 我是Android编程的新手,所以请多多包涵。

This Android app is a simple "Guess My Number" game in a blank activity. 这个Android应用程式是空白活动中的简单“猜我的号码”游戏。 The user is to guess from 1-100, enter their answer inside a EditText view, and submit it with a push of a button. 用户可以从1到100进行猜测,在EditText视图中输入答案,然后按一下按钮提交。 The design is fine, but getting it to work with OnClickListener is a hassle. 该设计很好,但是使其与OnClickListener一起使用很麻烦。 The app crashes on my GS3 as soon as I press the button. 按下按钮后,该应用在我的GS3上崩溃。 The most troubling part is getting the button to act and give outputs in the form of Toast. 最麻烦的部分是使按钮动作并以Toast的形式提供输出。

Attached is the code from MainActivity.java. 随附的是MainActivity.java中的代码。

I managed to pick up different snippets of code through StackOverflow as well as a bit of Java that I knew. 我设法通过StackOverflow以及我所知道的一些Java代码获取了不同的代码片段。 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. 首先,您的EditText的输入类型是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 . 此外,用户可以提交空白的编辑文本,当您调用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. 但是,如果用户输入了“ 3”,而您的计算值为“ 10”,则您的循环所做的就是一次又一次地确定3!= 10。 This causes the UI to freeze. 这将导致UI冻结。

Removing your while loop will allow the Toast to show: 删除while循环将使Toast显示:

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 : 然后在main.xml中,将inputType从phone更改为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. 您已在点击侦听器中为guess变量进行了无限循环,该变量一直在增加并且不会停止。

Just remove the do{...}while loop and then try out. 只需删除do{...}while循环,然后尝试即可。 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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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