【问题标题】:How to handle Number Format Exception in edittext OntextChanged如何处理edittext OntextChanged中的数字格式异常
【发布时间】:2019-04-22 11:29:49
【问题描述】:

我在我的应用程序中编辑了文本来操纵产品数量,但是如果我要更改数量,那么问题是在那个时候获得 NPE。要更改编辑文本中的数量,我需要删除现有的并需要添加一个新的,请检查以下代码

viewHolder.edt_product_qty.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

                }

                @Override
                public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {


                    quantity_count= Integer.parseInt(viewHolder.edt_product_qty.getText().toString());


                    buyNowList(user_id,"UserCart",0,stList.get(position).getCartList1().getId(),stList.get(position).getCartList1().getProdSku(),stList.get(position).getCartList1().getProdId(),quantity_count,0,qty_status,stList.get(position).getCartList1().getProdPrice());

                }

                @Override
                public void afterTextChanged(Editable editable) {

                }
            });

【问题讨论】:

  • 请分享您的完整代码和您的错误,这是适配器吗?
  • 是的,这是适配器..我正在使用 edittext 值来调用我的 api..so 来更改数量,它变得空并给出 NPE
  • 分享你的适配器代码完成!!!
  • 为什么不捕捉 Integer.parseInt 的 NumberFormatException 呢?如果 edt_product_qty 中没有数字(例如,当用户删除所有文本时),您的应用将崩溃。

标签: android android-edittext android-textwatcher


【解决方案1】:

使用trycatch

try {
  quantity_count = Integer.parseInt(viewHolder.edt_product_qty.getText().toString());
  buyNowList(user_id,"UserCart",0,stList.get(position).getCartList1().getId(),stList.get(position).getCartList1().getProdSku(),stList.get(position).getCartList1().getProdId(),quantity_count,0,qty_status,stList.get(position).getCartList1().getProdPrice());
} catch (NumberformatException e) {
  e.printStackTrace();
}

【讨论】:

  • 有什么办法可以检查,更新值大于现有值..例如..如果我从 2 更改为 5,那么 5 反之亦然
  • 在你的 aftertextchange 中保存这个 quantity_count 到另一个变量然后比较
  • 那你会怎么比较??
  • if (a>b) , if (a
  • if(!viewHolder.edt_product_qty.getText().toString().equals("")){ quantity_count= Integer.parseInt(viewHolder.edt_product_qty.getText().toString()); // } int tempqty=quantity_count; if(比较什么) { } else { }
【解决方案2】:

当您删除 editText 上的所有内容时,editText 将返回一个空字符串。因此,您将获得NPENumber Format Exception

你可以这样处理:

if(!viewHolder.edt_product_qty.getText().toString().equals("")){
   quantity_count= Integer.parseInt(viewHolder.edt_product_qty.getText().toString()); 
   buyNowList(user_id,"UserCart",0,stList.get(position).getCartList1().getId(),stList.get(position).getCartList1().getProdSku(),stList.get(position).getCartList1().getProdId(),quantity_count,0,qty_status,stList.get(position).getCartList1().getProdPrice());
}

比较初始值和当前值

首先您需要在beforeTextChanged 中分配一个变量作为初始值。

initalValue =viewHolder.edt_product_qty.getText().toString();

然后在onTextChanged:

if(!viewHolder.edt_product_qty.getText().toString().equals("")){
    quantity_count = Integer.parseInt(viewHolder.edt_product_qty.getText().toString()); // 
}if(initalValue>quantity_count)
{
    // code
}else
{
    //code
}

看看这个https://stackoverflow.com/a/20278708/5156075

【讨论】:

  • 我应该在哪里添加这个条件?在 ontextchanged 还是 aftertextchanged 里面?
  • onTextCanged.
  • 出现错误 java.lang.NumberFormatException: For input string: "" quantity_count= Integer.parseInt(viewHolder.edt_product_qty.getText().toString());在这条线上
  • 有什么办法可以检查,更新的值大于现有的值..例如..如果我从 2 更改为 5 则 5 反之亦然
  • 何时比较?单击其中一个按钮后?
【解决方案3】:
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
    try {
        quantity_count = Integer.parseInt(viewHolder.edt_product_qty.getText().toString()); 
        buyNowList(user_id, "UserCart", 0, stList.get(position).getCartList1().getId(), stList.get(position).getCartList1().getProdSku(), stList.get(position).getCartList1().getProdId(), quantity_count, 0, qty_status, stList.get(position).getCartList1().getProdPrice());    
    } catch (NumberFormatException e) {
        // maybe show some information to user in case of exception
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-04-11
    • 1970-01-01
    • 2011-12-28
    • 1970-01-01
    • 1970-01-01
    • 2011-09-18
    • 1970-01-01
    相关资源
    最近更新 更多