2017-04-13 5 views
-1

ユーザーがIntegerデータ型よりも大きな数値を入力すると、AlertDialogからNumberFormatExceptionをキャッチしようとしています。私は次のコードを試しましたが、私は成功を見出しませんでした。私は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これは、エラーが発生したときにアンドロイドスタジオでNumberFormatExceptionを見たときにわかったことです。次に、あなたはどんな例外を提案しますか? –

答えて

1

私はあなたのalertDialogで結果を処理し、例外を処理していない理由は、NumberFormatException

をキャッチしようとしています。

このコードを試してみてください。

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) { 

      } 
     } 
    }); 

ここであなたは、それはまたあなたの必要bounds..andであるかどうかをチェックするためにedittext出力文字列を処理することができ、簡単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がこのエラーをキャッチ – Vyacheslav

+1

残念ながら私のアプリは最低限のAPI 9に設定されており、Math.toIntExcact()にはAPI 24などが必要です。ありがとうございました:) –

+0

@JernejKalin大丈夫、よろしいですか? :) –

関連する問題