gpt4 book ai didi

java - 检查所有 EditText 是否为空时出现问题

转载 作者:行者123 更新时间:2023-12-02 01:28:25 24 4
gpt4 key购买 nike

我有 5 个 EditText,当我单击按钮时,我想检查它们是否全部不为空,以便从中获取数据并将其放入数据库,然后启动另一个 Activity 。如果全部为空,则会显示一条消息“您需要填写所有内容”。但是,当所有 EditText 为空并且我按下按钮时,应用程序崩溃。我该如何解决这个问题以及我该怎么做才能得到想要的结果? (代码尚未结束。)

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

最佳答案

崩溃发生在这里:

int year1 = Integer.parseInt(year2);

EditText 为空时,您将空字符串传递给 Integer.parseInt,这会导致 NumberFormatException。为了避免这种情况,请将解析代码移到验证 block 中。

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

关于java - 检查所有 EditText 是否为空时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56498817/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com