我该如何解决:java.util.UnknownFormatConversionException:Conversion ='''?

时间:2017-12-24 14:07:53

标签: java android android-studio

我是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);
    }
}

请帮助!!

1 个答案:

答案 0 :(得分:0)

如果未设置任何变量参数,则无需使用String.format()。在cleanCart()方法中,将第2行替换为: String query =“DELETE FROM OrderDetail”;

另外,您应该使用getWritableDatabase()来获取数据库对象,因为您实际上正在写入它。

编辑:用此替换你的addToCart()方法。

v = x.mul(x.columns).values
c = np.arange(1, x.shape[1] + 1)

df = pd.DataFrame(v, columns=c).add_prefix('VLUE') 
df

  VLUE1 VLUE2 VLUE3 VLUE4 VLUE5
0     A     B     C            
1     A           C     D     E
2                       D     E