这个项目是电子商务应用程序上的一项任务,我很乐意得到帮助。我正在尝试检查产品数量是否大于Firestore控制台中的购物车中的产品,但我一直存在NumberFormatException错误。下面是当用户试图增加给出错误的订单数量时的代码
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()
}
}
}调试控制台中的错误如下
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)减少订单数量的代码工作得很好,这与用户增加订单时相似。
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)
}
}发布于 2022-01-07 17:40:47
如果您试图将"“解析为int,它将通过相同的异常,在调用toInt之前确保字符串不是空的或空的。
https://stackoverflow.com/questions/70624899
复制相似问题