2016-08-09 37 views
3

我想显示计算textView(txtHasil)它正在运行,但当输入超过10应用程序突然强制关闭。这是我的代码:应用程序停止时输入editText超过10位

btnHitung.setOnClickListener(new View.OnClickListener() { 
    @Override 
    public void onClick(View view) { 

     //String plafond = NumberTextWatcherForThousand.trimCommaOfString(edtPlafond.getText().toString()); 
     String plafond = edtPlafond.getText().toString().trim(); 
     String jasa = edtJasa.getText().toString().trim(); 

     int edtPlafond = Integer.parseInt(plafond); 
     float edtJasa = Float.parseFloat(jasa); 

     double hasil = (edtPlafond * edtJasa)/100; 


     txtHasil.setText(""+hasil+"\nplafond: "+edtPlafond+"\nJasa: "+edtJasa); 
     //txtHasil.addTextChangedListener(new NumberTextWatcherForThousand((EditText) txtHasil)); 
    } 
} 

我一直尝试改变整型,浮点和双。我已阅读此链接:This program doesn't work properly for decimals more than 10 digits?但没有帮助。任何建议都会有所帮助。感谢

回答

4
Integer.parseInt(plafond); 

这是problem.It无法解析anythong大于Integer.MAX_VALUE

int edtPlafond; 
try { 

    edtPlafond = Integer.parseInt(plafond); 

} catch (NumberFormatException e) { 
    e.printStackTrace(); 
    // add proper error handling 
} 

最好是将有一个较长的价值 - 长...

long edtPlafond; 
try { 

    edtPlafond = Long.parseLong(plafond); 

} catch (NumberFormatException e) { 
    e.printStackTrace(); 
    // add proper error handling 
} 

而且通过在对话框中显示错误,以更好的方式处理错误的示例:

} catch (NumberFormatException e) { 
     new AlertDialog.Builder(getActivity()) 
      .setTitle("Error: incorrect number entered!") 
      .setMessage("The exact error is: " + e.getMessage()) 
      .setPositiveButton("Ok", 
       new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog, int b) { 
         dialog.cancel(); 
        } 
       }); 
      .create() 
      .show(); 
} 

注意:所有的转换需要这样的治疗...

+0

感谢,这比强制停止更好。但需要一点点修改: 使用(不是{ –

+1

对不起,是从手机打字,没有看到两个括号之间的区别...另外,增加了另一个片段,与长类型,应该能够持有更长的价值... – ppeterka

+0

这是否意味着开玩笑?对于不明白数据类型的人来说,有人帮助他们,他们不知道如何编写错误处理...我的头脑被吹了 –