简体   繁体   English

如何停止“强制关闭”错误

[英]How to stop the “force close” error

Iam giving some Values in EditText,but if i try to delete it using backspack button and to enter new value..It is getting ForceClosed.How to Rectify this Problem 我在EditText中给出一些值,但是如果我尝试使用backspack按钮删除它并输入新值。它正在强制关闭。如何解决此问题

My code is given below 我的代码如下

empty_cyl_recvd = (EditText) findViewById(R.id.empty_cyl_recvd);
 new_cyl_recvd = (EditText) findViewById(R.id.new_cyl_recvd);
 filled_cyl_unload = (EditText) findViewById(R.id.filled_cyl_unload);`enter code here`
  dmg_cyl_recvd = (EditText) findViewById(R.id.dmg_cyl_recvd);      

     total_dmg_cyl=(EditText)findViewById(R.id.sdff);
    total_empty_cyl=(EditText)findViewById(R.id.wertyu);    
    total_filled_cyl=(EditText)findViewById(R.id.gfhgftg);

 empty_cyl_recvd.addTextChangedListener(new TextWatcher()
         {

            @Override
            public void afterTextChanged(Editable arg0) {
                // TODO Auto-generated method stub

            }

            @Override
            public void beforeTextChanged(CharSequence arg0, int arg1,
                    int arg2, int arg3) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onTextChanged(CharSequence arg0, int arg1, int arg2,
                    int arg3) {

    int a=Integer.parseInt(empty_cyl_recvd.getText().toString());             int b=Integer.parseInt(Util.EMPTY_LIST.get(0).toString());
                int c=a+b;
                total_empty_cyl.setText(""+c);


                            }});

         filled_cyl_unload.addTextChangedListener(new TextWatcher()
         {

            @Override
            public void afterTextChanged(Editable s) {
                // TODO Auto-generated method stub

            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                    int after) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before,
                    int count) {

                int a=Integer.parseInt(filled_cyl_unload.getText().toString());
                int b=Integer.parseInt(Util.FILL_LIST.get(0).toString());
                int c=a+b;
                total_filled_cyl.setText(""+c);


         });


         dmg_cyl_recvd.addTextChangedListener(new TextWatcher(){

            @Override
            public void afterTextChanged(Editable s) {
                // TODO Auto-generated method stub

            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                    int after) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before,
                    int count) {

                int a=Integer.parseInt(dmg_cyl_recvd.getText().toString());
                int b=Integer.parseInt(Util.DAMAGE_LIST.get(0).toString());
                int c=a+b;
                                total_dmg_cyl.setText(""+c);


         });``

1st of all i would suggest you to use debugger, that would help you catch such issues quickly. 首先,我建议您使用调试器,这将帮助您快速发现此类问题。

in the code of onTextChange there are possibility of exceptions (NPE/NumberFormat/ArrayIndexOutOfBounds). 在onTextChange的代码中,可能会出现异常(NPE / NumberFormat / ArrayIndexOutOfBounds)。 So to put appropriate check around this code 所以要对此代码进行适当的检查

int a,b;
if(dmg_cyl_recvd.getText() != null && !dmg_cyl_recvd.getText().trim().equals("")) // also put it in try-catch for the case the input is not numeric
try{
    a=Integer.parseInt(dmg_cyl_recvd.getText().toString());
}catch(NumberFormatException ex){
//alert user about wrong input
}
if(Util.DAMAGE_LIST.size() > 0)//assuming it is a list, if not use method that gives the size of corresponding collection
    b=Integer.parseInt(Util.DAMAGE_LIST.get(0).toString());

Add proper exception handling to avoid Forced Close. 添加适当的异常处理以避免强制关闭。 You are probably getting NumberFormatException on Integer.parseInt(..) when it gets empty/invalid text. 当它获取空/无效文本时,可能在Integer.parseInt(..)上获取NumberFormatException。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM