有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

当输入的editText超过10位时,java应用程序停止

我试图显示calculation textView(txtHasil),它正在运行,但当输入超过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));
    }
}

我一直在尝试更改int、float和double。我已经读过这个链接:This program doesn't work properly for decimals more than 10 digits?但没有帮助。任何建议都会有帮助。谢谢


共 (1) 个答案

  1. # 1 楼答案

    Integer.parseInt(plafond);
    

    这就是问题所在。它不能解析大于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();
    }
    

    注意:所有转换都需要这样的处理