You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android购物车按钮点击崩溃求助:SQLite与Firebase环境下功能异常

Hey there, let's dig into why your app crashes when adding items to the cart. I spotted a few critical issues in your code that are likely causing the problem:

1. Syntax Error in INSERT SQL Statement (The Most Likely Culprit)

Look at your addToCart method in the Database class:

String query = String.format("INSERT INTO OrderDetail(ProductId,ProductName,Quantity,Price,)VALUES('%s','%s','%s','%s');", ...);

Notice the extra comma after Price in the column list? That's invalid SQL syntax, which will immediately throw an exception when you try to execute it. Fix it by removing that trailing comma:

String query = String.format("INSERT INTO OrderDetail(ProductId,ProductName,Quantity,Price)VALUES('%s','%s','%s','%s');", ...);

2. Using Readable Database for Write Operations

You're using getReadableDatabase() when inserting data. While this might work in most cases, it's designed for read operations—if the database hits a read-only state (like low disk space), this will fail. Replace it with getWritableDatabase() for all write operations (insert/update/delete):

SQLiteDatabase db = getWritableDatabase();

Apply this fix to the addToCart, cleanCart, and updateCart methods.

3. SQL Injection Risk & Fragile String Concatenation

Building SQL queries with string formatting is risky—if any product name or field contains a single quote ('), the query will break again. A safer approach is to use ContentValues, which handles escaping automatically:

public void addToCart(Order order) {
    SQLiteDatabase db = getWritableDatabase();
    ContentValues values = new ContentValues();
    values.put("ProductId", order.getProductId());
    values.put("ProductName", order.getProductName());
    values.put("Quantity", order.getQuantity());
    values.put("Price", order.getPrice());
    db.insert("OrderDetail", null, values);
    db.close(); // Close the database to avoid resource leaks
}

4. Mismatched Order Constructor Parameters

In your FoodDetail activity, you're creating an Order with 4 parameters:

new Order(foodId, currentFood.getName(), numberButton.getNumber(), currentFood.getPrice())

But in getCarts, you're constructing Order with 5 parameters (ID, ProductId, ProductName, Quantity, Price). Unless your Order class has an overloaded constructor that accepts 4 parameters (omitting the auto-incremented ID), this will cause a runtime error. Add this constructor to your Order class:

public Order(String productId, String productName, String quantity, String price) {
    this.ProductId = productId;
    this.ProductName = productName;
    this.Quantity = quantity;
    this.Price = price;
}

Since the ID is auto-generated by SQLite, you shouldn't pass it when inserting new cart items.

5. Trailing Comma in SQL Select Array

Your getCarts method has an extra comma in the column list:

String[] sqlSelect={"ID","ProductName","ProductId","Quantity","Price",};

While some compilers ignore this, it's better to remove it to avoid any potential issues:

String[] sqlSelect={"ID","ProductName","ProductId","Quantity","Price"};

After fixing these issues, test adding items to the cart again. If you still run into problems, share the updated LogCat error message and we can dig deeper!

内容的提问来源于stack exchange,提问作者Onix

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 07:34:36