首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >我不知道如何解决代码,因为我一直有java.lang.NumberFormatException: For输入字符串的反馈:"“

我不知道如何解决代码,因为我一直有java.lang.NumberFormatException: For输入字符串的反馈:"“
EN

Stack Overflow用户
提问于 2022-01-07 17:17:21
回答 1查看 69关注 0票数 0

这个项目是电子商务应用程序上的一项任务,我很乐意得到帮助。我正在尝试检查产品数量是否大于Firestore控制台中的购物车中的产品,但我一直存在NumberFormatException错误。下面是当用户试图增加给出错误的订单数量时的代码

代码语言:javascript
复制
 holder.itemView.findViewById<ImageButton>(R.id.ib_add_cart_item).setOnClickListener {
    // converting the carQuantity to Int from String
    val cartQuantity: Int = cartProductItemListModel.cart_quantity.toInt()

          
    // check if the cart_quantity is less than the stock_quantity
    if (cartQuantity < cartProductItemListModel.product_quantity.toInt()){
            val itemHashMap = HashMap<String, Any>()
            itemHashMap[Constants.CART_QUANTITY] = (cartQuantity + 1).toString()
            if (context is CartListActivity){
                context.showProgressDialogue("Updating Cart")
            }
            FirestoreClass().updateMyCartItem(context, cartProductItemListModel.id, itemHashMap)
    } else{
        if (context is CartListActivity){
            Toast.makeText(context, "Available stock for your order is (${cartProductItemListModel.product_quantity}). " +
                    "You can not add more than stock quantity", Toast.LENGTH_LONG).show()
        }
    }
}

调试控制台中的错误如下

代码语言:javascript
复制
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.hardextech.store, PID: 9807
java.lang.NumberFormatException: For input string: ""
    at java.lang.Integer.parseInt(Integer.java:627)
    at java.lang.Integer.parseInt(Integer.java:650)
    at com.hardextech.store.ui.activities.ui.adapter.CartItemListAdapter.onBindViewHolder$lambda-4(CartItemListAdapter.kt:126)
    at com.hardextech.store.ui.activities.ui.adapter.CartItemListAdapter.$r8$lambda$cL9k5ufbU_up2kCEhVgh9sgpi9I(Unknown Source:0)
    at com.hardextech.store.ui.activities.ui.adapter.CartItemListAdapter$$ExternalSyntheticLambda3.onClick(Unknown Source:4)
    at android.view.View.performClick(View.java:7044)
    at android.view.View.performClickInternal(View.java:7017)
    at android.view.View.access$3200(View.java:784)
    at android.view.View$PerformClick.run(View.java:26596)
    at android.os.Handler.handleCallback(Handler.java:873)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:193)
    at android.app.ActivityThread.main(ActivityThread.java:6819)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:497)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:912)

减少订单数量的代码工作得很好,这与用户增加订单时相似。

代码语言:javascript
复制
  holder.itemView.findViewById<ImageButton>(R.id.ib_remove_cart_item).setOnClickListener {
    // when the user clicks on the remove imageButton
    if (cartProductItemListModel.cart_quantity == "1"){
        // perform the same end function as when the user clicks on the delete button
        FirestoreClass().deleteItemFromCart(context, cartProductItemListModel.id)
       } else{
           // converting the cart_quantity from string to Int
           val cartQuantity: Int = cartProductItemListModel.cart_quantity.toInt()
        // creating an HashMap for updating the changes
           val itemHashMap = HashMap<String, Any>()
           itemHashMap[Constants.CART_QUANTITY] = (cartQuantity - 1).toString()

           //show the progress Dialogue
           if (context is CartListActivity){
               context.showProgressDialogue("Updating Cart")
           }
           FirestoreClass().updateMyCartItem(context, cartProductItemListModel.id, itemHashMap)
       }
}
EN

回答 1

Stack Overflow用户

发布于 2022-01-07 17:40:47

如果您试图将"“解析为int,它将通过相同的异常,在调用toInt之前确保字符串不是空的或空的。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70624899

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档