我想从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时,我明白了。那么你会建议什么例外? –