0

美好的一天,只是用相关的代码来简短地说,

如果我在我的活动中这样做:

     Intent i = getIntent();
          Bundle extras = i.getExtras();

          BitmapFactory.Options bf = new BitmapFactory.Options();
          bf.inSampleSize = 2;


          filename = extras.getString("pic_name");
          ImageView  iv = new ImageView(getApplicationContext());
          Bitmap bm = BitmapFactory.decodeFile(filename, bf);
          iv.setImageBitmap(bm);
          setContentView(iv);

  registerForContextMenu(iv);



public boolean onContextItemSelected(MenuItem item){
       // weatherimagedb = new WeatherImageDB(this);
        //weatherimagedb.open();

        ContentValues vals = new ContentValues();
        int selection = item.getItemId();
        switch(selection){
        case Holidays:

             vals.put(ImageDB.HOLIDAYS, filename);

        break;
        case Weather:
            vals.put(ImageDB.WEATHER, filename);

        break;
        }
             imagedb.tagImage(filename, vals); //logcat error is here

  return true;
}

在 imageDB 数据库类中,我有一个这样的方法:

public long tagImage(String pathname, ContentValues val){  
    return db.insert(DATABASE_TABLE, null, val);    //logcat error is here

}

我在数据库中收到此错误,但它不会使我的应用程序崩溃:

05-19 02:44:55.580: E/Database(3088): Error inserting 
05-19 02:44:55.580: E/Database(3088): android.database.sqlite.SQLiteException: near "null": syntax error: , while compiling: INSERT INTO imagetags(null)  VALUES(NULL);
05-19 02:44:55.580: E/Database(3088):   at android.database.sqlite.SQLiteCompiledSql.native_compile(Native Method)
05-19 02:44:55.580: E/Database(3088):   at android.database.sqlite.SQLiteCompiledSql.compile(SQLiteCompiledSql.java:92)
05-19 02:44:55.580: E/Database(3088):   at android.database.sqlite.SQLiteCompiledSql.<init>(SQLiteCompiledSql.java:65)
05-19 02:44:55.580: E/Database(3088):   at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:83)
05-19 02:44:55.580: E/Database(3088):   at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:41)
05-19 02:44:55.580: E/Database(3088):   at android.database.sqlite.SQLiteDatabase.compileStatement(SQLiteDatabase.java:1231)
05-19 02:44:55.580: E/Database(3088):   at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1658)
05-19 02:44:55.580: E/Database(3088):   at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1515)
05-19 02:44:55.580: E/Database(3088):   at com.MyApps.ImageUpdate.ImageDB.tagImage(ImageDB.java:82)
05-19 02:44:55.580: E/Database(3088):   at com.MyApps.ImageUpdate.ViewImage.onContextItemSelected(ViewImage.java:134)
05-19 02:44:55.580: E/Database(3088):   at android.app.Activity.onMenuItemSelected(Activity.java:2254)

请问我做错了什么?..我没有正确使用 ContentValues 吗?

谢谢你。

4

2 回答 2

1

难道你selection的既不是Holidays也不是Weather

于 2012-05-19T02:38:40.333 回答
0

在某些情况下,可能没有一个 case 开关是正确的,因此不会在“val”中添加任何内容。

于 2012-05-19T02:51:41.070 回答