2017-09-25 28 views
0

我是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> 
+0

此代碼中的按鈕在哪裏? – Ankita

+0

由於我只是從按鈕的XML中調用方法'guess()',因此我不必在此處保留按鈕。 –

+0

然後顯示你的XML。 @Sagun Raj Lage – Ankita

回答

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(); 
    } 
    } 
} 
+0

不客氣(y) –

0

在按鈕的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上的值..

+0

謝謝!但它已經存在於XML代碼中。 –

+0

顯示您的XML。 @SagunRajLage – Ankita

+0

顯示我的更新回答@SagunRajLage –

2

您不能將空白字符串轉換爲整數。 您檢查空字符串,如:

if(guessedtext.equalsIgnoreCase(""){ 
int guessInt = 0; 
}else{ 
int guessInt = Integer.parseInt(guessedtext); 
} 
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)); 
    } 
+0

他已經完成靜態導入activity_main。所以這不是必需的。 –

+0

好的等待我會檢查 –

0

嘗試實現OnClickListener與活動public class MainActivity extends AppCompatActivity implement Onclicklistener {}

0

定義你的按鈕在oncreate 它沒有定義任何地方

+0

我做到了,但它沒有幫助。 –

-1

enter image description here

見輸出敬酒猜測方法裏面寫清楚可見這裏。

一切都在你的代碼很好,只是你要你的活動綁定的分辯方式像的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; 

    } 
+0

他已經完成靜態導入activity_main。所以這不是必需的。 –

+0

好的,但這是從我身邊解決,它在這裏工作得很好。 – Ankita

+0

這似乎並沒有幫助。 –

相關問題