我需要在“小计”中显示我的购物车活动中所有商品价格的总计 我仍然收到错误消息:
java.lang.NullPointerException:尝试调用虚拟方法 空对象引用上的“ void android.widget.TextView.setText(double)”
我认为这是因为小计声明是错误的,但是我不知道为什么。
我的代码:
DROP TABLE IF EXISTS t;
CREATE TABLE t( id integer,f integer, x integer);
INSERT INTO t VALUES (1,1,2), (1,2,4), (1,3,5);
INSERT INTO t VALUES (2,1,10), (2,2,11), (2,3,20);
INSERT INTO t VALUES (3,1,40), (3,2,40), (3,3,40);
SELECT * from t;
SELECT MAX(ABS(sup.x - inf.x))
FROM (SELECT f, MAX(x) x from t WHERE id = 2 GROUP BY f) sup
INNER JOIN (select f, MIN(x) x FROM t WHERE id = 1 GROUP BY f) inf ON sup.f = inf.f
在MenuDetail类中,我获得诸如项目名称,价格,数量的信息:
public class CartAdapter extends RecyclerView.Adapter<CartAdapter.CartViewHolder>
{
Context context ;
private List<Cart> cartList;
private double price;
public CartAdapter(Context context, List<Cart> cartList) {
this.context = context;
this.cartList = cartList;
}
@NonNull
@Override
public CartViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(context).inflate(R.layout.cart_item_layout, parent, false);
// CartViewHolder viewHolder = new CartViewHolder(itemView, mData);
return new CartViewHolder(itemView);
}
@Override
public void onBindViewHolder(@NonNull CartViewHolder holder, final int position) {
double price = (Double.parseDouble(String.valueOf(cartList.get(position).price))
* Double.parseDouble(cartList.get(position).amount));
holder.cart_menu_amount.setText(new StringBuilder(cartList.get(position).amount).append(" x"));
holder.menu_name.setText(cartList.get(position).name);
// holder.menu_price.setText((new StringBuilder(String.valueOf(cartList.get(position).price))));
holder.menu_price.setText(new StringBuilder(String.valueOf(price)).append(" €"));
double subTotal = 0;
subTotal += (Double.parseDouble(String.valueOf(cartList.get(position).price))
* Double.parseDouble(cartList.get(position).amount));
holder.total_price_cart.setText((int) subTotal);
}
@Override
public int getItemCount() {
return cartList.size();
}
public class CartViewHolder extends RecyclerView.ViewHolder {
TextView menu_name;
TextView menu_price;
TextView cart_menu_amount;
TextView total_price_cart;
public CartViewHolder(View itemView) {
super(itemView);
total_price_cart = (TextView)itemView.findViewById(R.id.total_price_cart);
menu_name = (TextView)itemView.findViewById(R.id.cart_name);
menu_price= (TextView)itemView.findViewById(R.id.cart_price);
cart_menu_amount= (TextView) itemView.findViewById(R.id.cart_menu_amount);
}
}
}
答案 0 :(得分:1)
使用调试器检查您要在其中设置小计的textView,可能未将其附加到xml视图。 还要更改
holder.total_price_cart.setText((int)小计);
到
settext方法中期望的holder.total_price_cart.setText(String.valueOf(subTotal));
类型是字符串。
希望有帮助。