我是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()
,當我在guesseditText
一個空值click
按鈕,一個錯誤的應用程序崩潰說: 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>
此代碼中的按鈕在哪裏? – Ankita
由於我只是從按鈕的XML中調用方法'guess()',因此我不必在此處保留按鈕。 –
然後顯示你的XML。 @Sagun Raj Lage – Ankita