android 异常是:java.lang.NumberFormatException:对于输入字符串:“110.8500”

android Exception is: java.lang.NumberFormatException: For input string: "110.8500"

提问人:Abm 提问时间:10/6/2022 更新时间:10/6/2022 访问量:120

问:

在下面的 recyclerView 代码中,我正在增加/减少相应按钮的点击产品数量,但应用程序在点击增量时崩溃。我也在计算产品数量*价格,但它不能正常工作。我哪里做错了?如何计算产品价格?

@Override
public void onBindViewHolder(@NonNull RVOrderBrdAdapter.RVViewHolder holder, int position) {
    //getting the product of the specified position
    OrderBrdDataModel product = filteredList.get(position);

    //binding the data with the viewholder views
    holder.brd_value.setText(product.getBrdprice());
    holder.brd_product.setText(product.getBrdname());
    holder.brd_quantity.setText("1");

    holder.brd_increment.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            number = Integer.parseInt(holder.brd_quantity.getText().toString());
            if(holder.brd_quantity.getText().toString().equals("")){
                Toast.makeText(v.getContext(), "Quantity cannot be empty", Toast.LENGTH_SHORT).show();
            }else if(number >=9999){
                Toast.makeText(v.getContext(), "Quantity cannot be greater than 9999", Toast.LENGTH_SHORT).show();

            }else{
                number++;
                numVal =number++;

            }

            holder.brd_quantity.setText(""+number);
            String b = product.getBrdprice();

           holder.brd_value.setText(Integer.parseInt(b)*numVal);


        }
    });

    holder.brd_decrement.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            number = Integer.parseInt(holder.brd_quantity.getText().toString());
            if(holder.brd_quantity.getText().toString().equals("")){
                Toast.makeText(v.getContext(), "Quantity cannot be empty", Toast.LENGTH_SHORT).show();
            }else if(number<=0){
                Toast.makeText(v.getContext(), "Quantity cannot be less than 0", Toast.LENGTH_SHORT).show();
                number = 0;
            }else {
                number--;
                holder.brd_quantity.setText(""+number);
            }
        }
    });


}

@Override
public int getItemCount() {
    return filteredList.size();
}

@Override
public Filter getFilter() {

    return itemFilter;
}

public class RVViewHolder extends RecyclerView.ViewHolder {

    TextView brd_value, brd_product,brd_quantity,brd_increment,brd_decrement;

    public RVViewHolder(View itemView) {
        super(itemView);

        brd_value = itemView.findViewById(R.id.brd_value);
        brd_product = itemView.findViewById(R.id.brd_product);
        brd_quantity = itemView.findViewById(R.id.brd_quantity);
        brd_increment = itemView.findViewById(R.id.brd_increment);
        brd_decrement = itemView.findViewById(R.id.brd_decrement);

    }

}
public class ItemFilter extends Filter{

    @Override
    protected FilterResults performFiltering(CharSequence constraint) {
         ArrayList<OrderBrdDataModel> temp_filteredList= new ArrayList<>();
        for (int i=0;i<ordList.size();i++){
            if (ordList.get(i).isSelected()){
                temp_filteredList.add(ordList.get(i));
            }

        }
        FilterResults results = new FilterResults();
        results.values = temp_filteredList;
        return results;
    }

    @Override
    protected void publishResults(CharSequence constraint, FilterResults results) {
        filteredList = (ArrayList)results.values;
        System.out.println("RvFilter " + filteredList.size());
        notifyDataSetChanged();
    }
}

} 以下是我的崩溃日志

2022-10-06 12:06:46.633 11380-11380/com.live I/System.out: crash_report=Exception is: android.content.res.Resources$NotFoundException: String resource ID #0x6e
2022-10-06 12:15:50.147 12148-12148/com.live I/System.out: crash_report=Exception is: java.lang.NumberFormatException: For input string: "110.8500"
java android-recyclerview nullpointerexception

评论

2赞 Jens 10/6/2022
110.8500 不是整数。它是一个十进制值
0赞 Abm 10/6/2022
@Jens - 所以将 int 更改为 double 会解决错误吗?
0赞 Stultuske 10/6/2022
@Abm是的,请使用 Double.parseDouble。
0赞 Jens 10/6/2022
是的,这应该有效
0赞 Abm 10/6/2022
@Jens - holder.brd_value.setText(Double.parseDouble(b)*numVal);它说无法解析方法“setText(double)”

答: 暂无答案