[英]My app crashes whenever my EditText is empty and I press a button
問題是,每當我單擊應用程序中的按鈕時,只要我的一個或兩個EditText
為空,該應用程序就會崩潰。
這個想法是,我會寫在卡路里EditText
稱為caloriesIn,它將推出一個int
在caloriesOut
這是一個文本框。 “脂肪”也有同樣的想法。
綜上所述,問題是,如果我用卡路里寫一些東西,但是不寫任何脂肪,或者只是不寫任何東西,那么應用程序將崩潰。
我的代碼:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener( new View.OnClickListener() {
@Override
public void onClick(View view) {
EditText caloriesIn = (EditText) findViewById(R.id.caloriesIn);
EditText fatIn = (EditText) findViewById(R.id.fatIn);
TextView caloriesOut = (TextView) findViewById(R.id.caloriesOut);
TextView fatOut = (TextView) findViewById(R.id.fatOut);
int calories = Integer.parseInt(caloriesIn.getText().toString());
int fat = Integer.parseInt(fatIn.getText().toString());
int caloriesResult = calories;
int fatResult = fat;
caloriesOut.setText(caloriesResult + "");
fatOut.setText(fatResult + "");
}
});
}
}
崩潰報告:
03月22日17:20:02.512 22193-22193 / ottolopez.healthynote I /編舞:跳過了47幀! 該應用程序可能在其主線程上做太多工作。 03月22日17:20:02.556 22193-22193 / ottolopez.healthynote V /視圖:dispatchProvideAutofillStructure():未布局,忽略了10個孩子的1073741833 03-22 17:20:02.561 22193-22193 / ottolopez.healthynote I / AssistStructure :扁平化的最終輔助數據:2936字節,包含1個窗口,11個視圖03-22 17:20:05.047 22193-22193 / ottolopez.healthynote D / AndroidRuntime:關閉VM 03-22 17:20:05.049 22193-22193 / ottolopez .healthynote E / AndroidRuntime:致命例外:主進程:ottolopez.healthynote,PID:22193 java.lang.NumberFormatException:對於輸入字符串:java.lang.Integer.parseInt(Integer.java:620)處的“” .Integer.parseInt(Integer.java:643)位於ottolopez.healthynote.MainActivity $ 1.onClick(MainActivity.java:28)在android.view.View.performClick(View.java:6294)在android.view.View $ PerformClick .run(View.java:24770)在android.os.Handler.handleCallback(Handler.java:790)在android.os.Handler.dispatchMessage(Handler.java:99)在android.os.Looper.loop(Looper。 java:164)在androi d.app.ActivityThread.main(ActivityThread.java:6494)位於com.android.internal.os.RuntimeInit $ MethodAndArgsCaller.run(RuntimeInit.java:438)處的java.lang.reflect.Method.invoke(本機方法) com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
在您的活動中定義此方法:
public boolean isParsable(String input){
boolean parsable = true;
try{
Integer.parseInt(input);
}catch(NumberFormatException e){
parsable = false;
}
return parsable;
}
然后像這樣更新您的代碼:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener( new View.OnClickListener() {
@Override
public void onClick(View view) {
EditText caloriesIn = (EditText) findViewById(R.id.caloriesIn);
EditText fatIn = (EditText) findViewById(R.id.fatIn);
TextView caloriesOut = (TextView) findViewById(R.id.caloriesOut);
TextView fatOut = (TextView) findViewById(R.id.fatOut);
String caloriesString = caloriesIn.getText().toString();
int calories = 0;
if (isParsable(caloriesString)){
calories = Integer.parseInt(caloriesString);
int caloriesResult = calories;
caloriesOut.setText(caloriesResult + "");
}
String fatString = caloriesIn.getText().toString();
int fat = 0;
if (isParsable(fatString)){
fat = Integer.parseInt(fatString);
int fatResult = fat;
fatOut.setText(fatResult + "");
}
}
});
}
public static boolean isParsable(String input){
boolean parsable = true;
try{
Integer.parseInt(input);
}catch(NumberFormatException e){
parsable = false;
}
return parsable;
}
}
希望這可以幫助
問題是Integer.parseInt()
不能處理不是數字的空字符串。 因此,您需要檢查文本是否為空,然后使用Integer.parseInt()
。 您也可以在使用parseInt之前使用isDigitsOnly方法。
int calories = Integer.parseInt(caloriesIn.getText().toString());
int fat = Integer.parseInt(fatIn.getText().toString());
主要問題是在這些行中,如果您在此處傳遞任何字符或將其保留為空白,則會產生錯誤,以解決這些問題,並將這些行添加到代碼中
String calIn = caloriesIn.getText().toString(); //first take input in String for checking
String fatInStr = fatIn.getText().toString(); //first take input in String for checking
if (calIn.isEmpty()) {
Toast.makeText(...);//inform user that calories input field is empty
return;
} else if (fatInStr.isEmpty()) {
Toast.makeText(...);//inform user that fat input field is empty
return;
} else if (!calIn.matches("[0-9]+")) {
Toast.makeText(...);//inform user that calories input field contains invalid data
return;
} else if (!fatInStr.matches("[0-9]+")) {
// Toast.makeText(...);//inform user that fat input field contains invalid data
return;
}
int calories = Integer.parseInt(calIn.trim()); // trim method removes any leading and trailing spaces
int fat = Integer.parseInt(fatInStr.trim());
現在,當文本為空且不是數字時,應用是安全的:)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.