单击按钮提交空输入时Android应用程序崩溃

时间:2017-09-25 05:29:53

标签: android

我是Android的新手,我正在创建一个系统生成随机数的游戏,用户必须猜测它。 代码如下:

package np.com.sagunrajlage.higherorlower;

import android.content.Intent;
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.Toast;

import java.util.Random;

import static np.com.sagunrajlage.higherorlower.R.layout.activity_main;

public class MainActivity extends AppCompatActivity {
    int randomnumber, count;
    TextView guessnumberTextView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(activity_main);
        guessnumberTextView = (TextView) findViewById(R.id.guessnumberTextView);
        Random rand = new Random();
        randomnumber = rand.nextInt(20) + 1;

    }

    public void makeToast(String string){
        Toast.makeText(this, string, Toast.LENGTH_LONG).show();
    }



    public void guess(View view){
        count++;
        EditText guesseditText = (EditText) findViewById(R.id.guesseditText);
        String guessedtext = guesseditText.getText().toString();
        int guessInt = Integer.parseInt(guessedtext);

        if(guessInt>randomnumber) {
            makeToast("Guess a lower number!");
        }
        else if(guessInt<randomnumber){
            makeToast("Guess a higher number!");
        }
        else{
            makeToast("You got it! "+ randomnumber +" was the number. Now guess the new number I chose.");
            Random rand = new Random();
            randomnumber = rand.nextInt(20) + 1;
        }
        guesseditText.setText("");
        guessnumberTextView.setText("Number of guesses: "+Integer.toString(count));
    }
}

此处,onClick事件点击guess(),当我click guesseditText处的值为空的按钮时,应用程序崩溃并显示错误消息: java.lang.IllegalStateException: Could not execute method for android:onClick

XML是:

<?xml version="1.0" encoding="utf-8"?>
<android.widget.RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="np.com.sagunrajlage.higherorlower.MainActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <TextView
            android:id="@+id/textView2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginEnd="8dp"
            android:layout_marginLeft="8dp"
            android:layout_marginRight="8dp"
            android:layout_marginStart="8dp"
            android:layout_marginTop="15dp"
            android:text="I'm thinking of a number between 1 and 20."
            android:textAppearance="@style/TextAppearance.AppCompat.Body1"
            app:layout_constraintHorizontal_bias="0.0"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            tools:layout_editor_absoluteY="45dp" />

        <android.support.constraint.Guideline
            android:id="@+id/guideline"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            app:layout_constraintGuide_begin="20dp"
            tools:layout_editor_absoluteX="0dp"
            tools:layout_editor_absoluteY="20dp" />

        <TextView
            android:id="@+id/textView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="9dp"
            android:layout_marginEnd="8dp"
            android:layout_marginLeft="8dp"
            android:layout_marginRight="8dp"
            android:layout_marginStart="8dp"
            android:layout_marginTop="20dp"
            android:text="Can you guess it?"
            android:textAppearance="@style/TextAppearance.AppCompat.Display1"
            app:layout_constraintBottom_toTopOf="@+id/guesseditText"
            app:layout_constraintHorizontal_bias="0.0"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/textView2"
            app:layout_constraintVertical_bias="0.115" />

        <EditText
            android:id="@+id/guesseditText"
            android:layout_width="match_parent"
            android:layout_height="60dp"
            android:layout_marginBottom="31dp"
            android:layout_marginTop="45dp"
            android:ems="10"
            android:hint="Enter the number here"
            android:inputType="number"
            app:layout_constraintBottom_toTopOf="@+id/guessbutton"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent" />

        <TextView
            android:id="@+id/guessnumberTextView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Number of Guesses: 0"
            android:textAlignment="center"
            android:textAppearance="@style/TextAppearance.AppCompat.Display1" />

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center_horizontal">

            <Button
                android:id="@+id/guessbutton"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="50dp"
                android:layout_marginRight="50dp"
                android:layout_marginTop="30dp"
                android:onClick="guess"
                android:text="guess"
                android:textAlignment="center"
                app:layout_constraintHorizontal_bias="0.498"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintRight_toRightOf="parent"
                tools:layout_editor_absoluteY="303dp" />
        </RelativeLayout>

    </LinearLayout>

</android.widget.RelativeLayout>

7 个答案:

答案 0 :(得分:2)

您无法将空字符串转换为整数。 你检查空白字符串,如:

if(guessedtext.equalsIgnoreCase(""){
 int guessInt = 0;
}else{
 int guessInt = Integer.parseInt(guessedtext);
}

答案 1 :(得分:0)

在Button的xml代码中尝试android:onClick="guess"并在onCreate方法中定义EditText ...

 EditText guesseditText;
 Button guessbutton;

 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(activity_main);
    guessnumberTextView = (TextView) findViewById(R.id.guessnumberTextView);
    guesseditText = (EditText) findViewById(R.id.guesseditText);
    guessbutton=(Button)findViewById(R.id.guessbutton);
    Random rand = new Random();
    randomnumber = rand.nextInt(20) + 1;

}

然后你可以在任何地方获得这个edittext的值..

答案 2 :(得分:0)

我认为你应该改变这一行:

 setContentView(activity_main);

setContentView(R.layout.activity_main);

更改您的代码如下:

Button btn = (Button) findViewById(R.id.yourButtonFromXml);
onCreate()

中的

btn.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        guess(v);
    }
});

-

 public void guess(View view){
        count++;
        EditText guesseditText = (EditText) findViewById(R.id.guesseditText);
        String guessedtext = guesseditText.getText().toString();    
        int guessInt = Integer.parseInt(guessedtext.getZText().toString().trim());   //you missed guessedtext.getZText().toString()

        if(guessInt>randomnumber) {
            makeToast("Guess a lower number!");
        }
        else if(guessInt<randomnumber){
            makeToast("Guess a higher number!");
        }
        else{
            makeToast("You got it! "+ randomnumber +" was the number. Now guess the new number I chose.");
            Random rand = new Random();
            randomnumber = rand.nextInt(20) + 1;
        }
        guesseditText.setText("");
        guessnumberTextView.setText("Number of guesses: "+Integer.toString(count));
    }

答案 3 :(得分:0)

试试这个:)这对我来说是运行的:)

import static teratomo.testapp.R.layout.activity_main;

public class MainActivity extends AppCompatActivity {

  int randomnumber, count;
  TextView guessnumberTextView;
  EditText guesseditText;
  String guessedtext;
  int guessInt = 0;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(activity_main);
    guessnumberTextView = (TextView) findViewById(R.id.guessnumberTextView);
    guesseditText = (EditText) findViewById(R.id.guesseditText);
    Random rand = new Random();
    randomnumber = rand.nextInt(20) + 1;
  }

  public void makeToast(String string){
    Toast.makeText(this, string, Toast.LENGTH_LONG).show();
  }



  public void guess(View view){
    count++;
    if (guesseditText.getText().length() > 0) {
      guessInt = Integer.parseInt(guesseditText.getText().toString());
      if(guessInt > randomnumber) {
        makeToast("Guess a lower number!");
      }
      else if(guessInt < randomnumber){
        makeToast("Guess a higher number!");
      }
      else{
        makeToast("You got it! "+ randomnumber +" was the number. Now guess the new number I chose.");
        Random rand = new Random();
        randomnumber = rand.nextInt(20) + 1;
      }
      guesseditText.setText("");
      guessnumberTextView.setText("Number of guesses: "+Integer.toString(count));
    } else {
      Toast.makeText(this, "Please fill field", Toast.LENGTH_SHORT).show();
    }
  }
}

答案 4 :(得分:0)

尝试使用您的活动public class MainActivity extends AppCompatActivity implement Onclicklistener {}

实施Onclicklistener

答案 5 :(得分:0)

在oncreate中定义按钮

未定义任何地方

答案 6 :(得分:-1)

enter image description here

查看输出 guess 方法中写的 toast 在此处清晰可见。

您的代码中的一切都很好,只是您应该以严格的方式绑定您的活动,例如 setContentView(R.layout.activity_main)而不是 setContentView(activity_main);

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        guessnumberTextView = (TextView) findViewById(R.id.guessnumberTextView);
        Random rand = new Random();
        randomnumber = rand.nextInt(20) + 1;

    }