我有5个EditText,当我单击按钮时,我想检查它们是否都为空,以便从中获取数据并将其放入数据库中,然后开始另一个活动。如果全部为空,将显示一条消息,“您需要填写所有内容”。但是,当所有EditTexts为空并且我按下按钮时,应用程序崩溃。我该如何解决这个问题,我该怎么做才能获得理想的结果? (代码尚未结束。)
public class Main2Activity extends AppCompatActivity {
DatabaseHelper myDB;
Button btnAdd;
EditText editText1,editText2,editText3,editText4,editText5;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
editText1 = (EditText) findViewById(R.id.name);
editText2 = (EditText) findViewById(R.id.year);
editText3 = (EditText) findViewById(R.id.month);
editText4 = (EditText) findViewById(R.id.day);
editText5 = (EditText) findViewById(R.id.price);
btnAdd = (Button) findViewById(R.id.btn_add);
myDB = new DatabaseHelper(this);
btnAdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String name1 = editText1.getText().toString();
String year2 = editText2.getText().toString();
int year1 = Integer.parseInt(year2);
String month2 = editText3.getText().toString();
int month1 = Integer.parseInt(month2);
String day2 = editText4.getText().toString();
int day1 = Integer.parseInt(day2);
String price2 = editText5.getText().toString();
int price1 = Integer.parseInt(price2);
if (name1.length() != 0 && year2.length() != 0 && month2.length() != 0 && day2.length() != 0 && price2.length() != 0) {
AddData(name1, year1, month1, day1, price1);
editText1.setText("");
editText2.setText("");
editText3.setText("");
editText4.setText("");
editText5.setText("");
Intent intent = new Intent(Main2Activity.this, MainActivity.class);
startActivity(intent);
} else {
Toast.makeText(Main2Activity.this, "You need to fill everything", Toast.LENGTH_LONG).show();
}
}
});
}
public void AddData(String name1, int year1, int month1, int day1, int price1) {
boolean insertData = myDB.addData(name1,year1,month1,day1,price1);
if(insertData==true){
Toast.makeText(this, "Data Successfully Inserted!", Toast.LENGTH_LONG).show();
}else{
Toast.makeText(this, "Something went wrong :(.", Toast.LENGTH_LONG).show();
}
}
}
答案 0 :(得分:1)
崩溃发生在这里:
int year1 = Integer.parseInt(year2);
当EditText
为空时,您正在向Integer.parseInt
传递一个空字符串,结果为NumberFormatException
。为了避免这种情况,请将解析代码移到验证块中。
@Override
public void onClick(View v) {
String name1 = editText1.getText().toString();
String year2 = editText2.getText().toString();
String month2 = editText3.getText().toString();
String day2 = editText4.getText().toString();
String price2 = editText5.getText().toString();
if (name1.length() != 0 && year2.length() != 0 && month2.length() != 0 && day2.length() != 0 && price2.length() != 0) {
// Move parsing code here
int year1 = Integer.parseInt(year2);
int month1 = Integer.parseInt(month2);
int day1 = Integer.parseInt(day2);
int price1 = Integer.parseInt(price2);
AddData(name1, year1, month1, day1, price1);
editText1.setText("");
editText2.setText("");
editText3.setText("");
editText4.setText("");
editText5.setText("");
Intent intent = new Intent(Main2Activity.this, MainActivity.class);
startActivity(intent);
} else {
Toast.makeText(Main2Activity.this, "You need to fill everything", Toast.LENGTH_LONG).show();
}
}
答案 1 :(得分:0)
仅在向所有您的editText输入文本后,您才允许用户按下按钮:
if (all editTexts got text inside) {
//not all editTexts got text inside them, user cant press the button
button.setClickable(false);
}
else{
//all editTexts got text inside them, user can now press the button
button.setClickable(true);
}
注意-我在if语句中写的“所有editTexts都包含文本”。