簡體   English   中英

當 EditText 為空時 Android 應用程序崩潰

[英]Android application crashes when EditText is empty

我有一個問題,我無法解決,老師的解決方案不起作用。

問題是當我單擊“檢查答案”並且EditText沒有寫入任何內容時,程序崩潰。 我在 Android Studio 工作。

爪哇代碼:

public class Main extends AppCompatActivity {
    int a, b, c, d;
    Button Enter, NewExercise, EndLesson;
    EditText et;
    TextView tv;
    ImageView iv;
    String st;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tv = (TextView) findViewById(R.id.tv);
        et = (EditText) findViewById(R.id.et);
        iv = (ImageView) findViewById(R.id.iv);
        a = (int)(10 * Math.random());
        b = (int)(10 * Math.random());
        c = a + b;
        tv.setText("Exmaple: " + a + " + " + b);
        et.setText("" + c);
    }

    public void Show(View view) {
        a = (int)(10 * Math.random());
        b = (int)(10 * Math.random());
        c = a + b;
        tv.setText("" + a + " + " + b);
        et.setText("");
    }

    public void Check(View view) {
        st = et.getText().toString();
        d = Integer.parseInt(st);
        if(0 == et.length()) {
            Toast.makeText(this,"Please, enter the answer",Toast.LENGTH_LONG).show();
        }
        if (d == c) {
            iv.setImageResource(R.drawable.t);
            Toast.makeText(this, "Right", Toast.LENGTH_LONG).show();
        }
        else {
            Toast.makeText(this, "False", Toast.LENGTH_LONG).show();
            iv.setImageResource(R.drawable.f);
            et.setText("");
        }
    }
}

xml:

<TextView
    android:layout_width="wrap_content"
    android:textSize="30dp"
    android:textColor="#000000"
    android:layout_height="wrap_content"
    android:id="@+id/tv" />

<ImageView
    android:layout_width="match_parent"
    android:layout_height="71dp"
    app:srcCompat="@drawable/eq"
    android:id="@+id/eq" />

<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:inputType="number"
    android:textSize="30dp"
    android:hint="Enter the answer"
    android:ems="10"
    android:maxLength="3"
    android:gravity="center"
    android:id="@+id/et"
    android:layout_weight="0.13" />

<ImageView
    android:layout_width="match_parent"
    android:layout_height="48dp"
    app:srcCompat="@drawable/t"
    android:id="@+id/iv"
    android:layout_weight="0.12" />

<Button
    android:text= "Check the answer"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:onClick="Check"
    android:id="@+id/CheckTheAnswer" />

<Button
    android:text= "New Exercise"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:onClick="Show"
    android:id="@+id/NewExercise" />

<Button
    android:text="End lesson"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:onClick="Exit"
    android:id="@+id/EndLesson" />

我為我的英語感到抱歉。 提前謝謝你。

那是因為您正在將空字符串(ie "")解析為導致崩潰的 int。

使用 try-catch 將 String 解析為 int 時應小心處理異常,因為如果 String 無法解析為 int 則拋出 Runtime 錯誤,這將導致應用程序崩潰。

使用此檢查 EditText 是否為空:

protected boolean isEmpty(EditText editText) {
        return (editText.getText().toString().equals(""));
    }

希望這會有所幫助。

嘗試放置一個空檢查器。 示例:

if(et != null){
  st = et.getText().toString().trim();
       d = Integer.parseInt(st);
}

我還添加了 trim() 方法來修剪尾隨和前導空格。

感謝您的回答,但只有這個解決方案有效。

if (st.equals(""))
    {
  vbsays.setText("< First, you need to enter something into the text box, and then check your answer. Try!");
    }
    else 
{
        d = Integer.parseInt(st);
}

getText() 的返回類型是可編輯的,因此我們需要使用 toString() 將其轉換為字符串。

if(t1.getText().toString().equals("")// t1 is Edittext(class) object
   {
       Toast.makeText(MainActivity.this, "my Message", Toast.LENGTH_LONG).show();
   }
else
   {
       //do ur operation
   }  

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM