我想從AlertDialog中捕獲NumberFormatException,如果用戶輸入的數字大於Integer數據類型可以處理的數字。我已經嘗試了下面的代碼,但我沒有找到任何成功。我正在嘗試捕獲setOnItemClickListener方法中的異常。從AlertDialog輸入中捕獲NumberFormatException
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, final int position, long id) throws RuntimeException{
try{
builder= new AlertDialog.Builder(IzbiraHrane.this);
builder.setTitle("Enter your quantity");
// Set up the input
final EditText input = new EditText(IzbiraHrane.this);
// Specify the type of input expected; this, for example, sets the input as a password, and will mask the text
input.setInputType(InputType.TYPE_CLASS_NUMBER);
builder.setView(input);
// Set up the buttons
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
kolicina = input.getText().toString();
Intent returnIntent = new Intent();
returnIntent.putExtra("kolicina",kolicina);
returnIntent.putExtra("id",id_ji.get(position));
setResult(Activity.RESULT_OK,returnIntent);
finish();
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
builder.show();
}catch(Exception e){
Toast.makeText(IzbiraHrane.this, "something", Toast.LENGTH_SHORT).show();
}
}
});
我也是Android和Java的新手,所以請在你的回答中考慮這個問題:) –
@OusmaneMahyDiaw這就是當我在錯誤發生時在android studio上看到NumberFormatException時,我明白了。那麼你會建議什麼例外? –