我是Android工作室的新手,我最近一直在开发一个应用程序,允许您下订单,并为了订购您添加的东西到购物车,然后可以从您的帐户页面查看详细信息。单击购物车或尝试将项目添加到购物车时,应用程序崩溃并显示:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.sakaaz.sakaaz, PID: 30731
java.util.UnknownFormatConversionException: Conversion = '''
at java.util.Formatter$FormatSpecifier.conversion(Formatter.java:2730)
at java.util.Formatter$FormatSpecifier.<init>(Formatter.java:2759)
at java.util.Formatter$FormatSpecifierParser.<init>(Formatter.java:2591)
at java.util.Formatter.parse(Formatter.java:2524)
at java.util.Formatter.format(Formatter.java:2472)
at java.util.Formatter.format(Formatter.java:2426)
at java.lang.String.format(String.java:2626)
at edmt.dev.androidsakaaz.Database.Database.addToCart(Database.java:55)
at edmt.dev.androidsakaaz.MakeupDetail$1.onClick(MakeupDetail.java:59)
at android.view.View.performClick(View.java:6257)
at android.view.View$PerformClick.run(View.java:23705)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6836)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)
我不知道出了什么问题。这是我的数据库类:
public class Database extends SQLiteAssetHelper {
private static final String DATABASE_NAME = "sakaazDB.db";
private static final int DB_VER=1;
public Database(Context context) {
super( context, DATABASE_NAME, null , DB_VER );
}
public List<Order> getCarts()
{
SQLiteDatabase db=getReadableDatabase();
SQLiteQueryBuilder qb= new SQLiteQueryBuilder();
String[] sqlSelect={"ProductName", "ProductId", "Quantity", "Price", "Discount"};
String sqlTable="OrderDetail";
qb.setTables( sqlTable );
Cursor c = qb.query(db,sqlSelect,null,null,null,null,null);
final List<Order> result = new ArrayList<>();
if (c.moveToFirst())
{
do{
result.add( new Order( c.getString(c.getColumnIndex("ProductId")),
c.getString(c.getColumnIndex("ProductName")),
c.getString(c.getColumnIndex("Quantity")),
c.getString(c.getColumnIndex("Price")),
c.getString(c.getColumnIndex("Discount"))
));
}while (c.moveToNext());
}
return result;
}
public void addToCart (Order order)
{
SQLiteDatabase db = getReadableDatabase();
String query = String.format("INSERT INTO OrderDetail(ProductId, ProductName, Quantity, Price, Discount) VALUES ('s%','s%','s%','s%','s%');",
order.getProductId(),
order.getProductName(),
order.getQuantity(),
order.getPrice(),
order.getDiscount());
db.execSQL(query);
}
public void cleanCart()
{
SQLiteDatabase db = getReadableDatabase();
String query = String.format("DELETE FROM OrderDetail");
db.execSQL(query);
}
}
请帮忙!!
如果未设置任何变量参数,则无需使用String.format()。在cleanCart()方法中,将第2行替换为:String query =“DELETE FROM OrderDetail”;
另外,您应该使用getWritableDatabase()来获取数据库对象,因为您实际上正在写入它。
编辑:用这个替换你的addToCart()方法。
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();
values.put("Discount", order.getDiscount();
db.insert("OrderDetail", null, values);
}