2017-04-13 22 views
-1

我想从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(); 
      } 
     } 
    }); 
+0

我也是Android和Java的新手,所以请在你的回答中考虑这个问题:) –

+0

@OusmaneMahyDiaw这就是当我在错误发生时在android studio上看到NumberFormatException时,我明白了。那么你会建议什么例外? –

回答

1

我试图赶上NumberFormatException的

为什么不处理您alertDialog结果和处理异常。

试试这个代码:

int number = 0; //declare varible 

    // Set up the buttons 
    builder.setPositiveButton("OK", new DialogInterface.OnClickListener() { 
     @Override 
     public void onClick(DialogInterface dialog, int which) { 
      try { 
       if(input.getString().toString().Trim().lenght()>0) //not empty 
       { 
        if(Integer.parseInt(input.gettext().toString()) <= 100000) //your Int bounds 
        { 
         kolicina = input.getText().toString(); 
         number = Integer.parseInt(kolicina); 
         Intent returnIntent = new Intent(); 
         returnIntent.putExtra("kolicina",kolicina); 
         returnIntent.putExtra("id",id_ji.get(position)); 
         setResult(Activity.RESULT_OK,returnIntent); 
         finish(); 
        } 
       } 
      } catch (NumberFormatException e) { 

      } 
     } 
    }); 

在这里你可以处理edittext输出字符串来检查它是否在你的bounds..and也可以很容易地捕捉Exception要求。

+0

我写了同样的。尽管如此,你的答案我们正确 – Vyacheslav

+0

@Vyacheslav你的回答也是正确的,我认为..我只是添加一些细节... – rafsanahmad007

0

我试图如果 用户输入比整数数据类型可以处理更大的数捕捉从AlertDialog NumberFormatException异常。

您可以使用,以便在Math.toIntExact()的方法来检查是否存在整数溢出/下溢。

实施例,沿着此线的东西:

try { 
    int myValue = Math.toIntExact(Long.parseLong(kolicina)); 
    // do something 
} catch (ArithmeticException e) { 
    // if it gets here then there is integer overflow/underflow. 
} catch(NumberFormatException e){ 
    // if it gets here then the data entered is not a valid number. 
} 

Math.toIntExact() - 返回长参数的值;如果值溢出int,则抛出 异常。

+0

Numberformatexception捕获此erroe – Vyacheslav

+1

不幸的是我的应用程序设置为最低的API 9和Math.toIntExcact()需要API 24或我明白。感谢您的时间:) –

+0

@JernejKalin好吧,不客气。 :) –