我做數據庫的程序。我創建了三個編輯文本,用於添加員工ID,姓名和電話號碼。問題是當我按下添加按鈕而沒有插入任何值的應用程序崩潰。我該如何處理?以下是我寫的代碼。如何在Android上編輯文本代碼添加驗證?
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()){
case R.id.btn_add:
Model india = new Model();
india.id=Integer.parseInt(edit_empid.getText().toString()); // convet editable object from get text to string and then into integer
india.name = edit_name.getText().toString();
india.phone = edit_phone.getText().toString();
db.addcontact(india);
list = db.getAllContacts();
print(list);
edit_empid.setText(" ");
edit_name.setText(" ");
edit_phone.setText(" ");
break;
case R.id.btn_delete:
long id = Long.parseLong(edit_empid.getText().toString());
db.deletecontact(id);
list = db.getAllContacts();
print(list);
break;
case R.id.btn_update:
Model Japan = new Model();
Japan.id=Integer.parseInt(edit_empid.getText().toString().trim());
Japan.name = edit_name.getText().toString();
Japan.phone = edit_phone.getText().toString();
int result = db.updatecontact(Japan);
System.out.println("Print the update result " + result);
list = db.getAllContacts();
print(list);
break;
case R.id.btn_clear:
edit_empid.setText(" ");
edit_name.setText(" ");
edit_phone.setText(" ");
break;
}
}
}