我有两个文件都使用特定的符号类“Car”。我在整个项目中选择了这个作为一种反复出现的模式。
我的错误:
DBhelper.java:
package com.example.brad.myapplication;
/**
* Created by Brad on 21/07/2014.
*/
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.graphics.Bitmap;
import android.view.View;
import android.view.TextureView;
public class DBhelper {
public static final String CAR_ID = "id";
public static final String CAR_PHOTO = "photo";
private DatabaseHelper mDbHelper;
private SQLiteDatabase mDb;
private static final String DATABASE_NAME = "CarDB.db";
private static final int DATABASE_VERSION = 1;
private static final String CARS_TABLE = "Cars";
private static final String CREATE_CARS_TABLE = "create table "
+ CARS_TABLE + " (" + CAR_ID
+ " integer primary key autoincrement, " + CAR_PHOTO
+ " blob not null);";
private final Context mCtx;
private static class DatabaseHelper extends SQLiteOpenHelper {
DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_CARS_TABLE);
}
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + CARS_TABLE);
onCreate(db);
}
}
public void Reset() {
mDbHelper.onUpgrade(this.mDb, 1, 1);
}
public DBhelper(Context ctx) {
mCtx = ctx;
mDbHelper = new DatabaseHelper(mCtx);
}
public DBhelper open() throws SQLException {
mDb = mDbHelper.getWritableDatabase();
return this;
}
public void close() {
mDbHelper.close();
}
public void insertCarDetails(**Car** car) {
ContentValues cv = new ContentValues();
cv.put(CAR_PHOTO, Utility.getBytes(car.getBitmap()));
mDb.insert(CARS_TABLE, null, cv);
}
public **Car** retriveCarDetails() throws SQLException {
Cursor cur = mDb.query(true, CARS_TABLE, new String[] { CAR_PHOTO}, null, null, null, null, null, null);
if (cur.moveToFirst()) {
byte[] blob = cur.getBlob(cur.getColumnIndex(CAR_PHOTO));
cur.close();
return new Car(Utility.getPhoto(blob));
}
cur.close();
return null;
}
}
InsertandRetriveBlobData:
package com.example.brad.myapplication;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
/**
* Created by Brad on 20/07/2014.
*/
public class InsertandRetriveBlobData extends MyActivity {
private DBhelper DbHelper;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
DbHelper = new DBhelper(this);
**Car** car_One = new Car(BitmapFactory.decodeResource(
getResources(), R.drawable.photo), 1);
DbHelper.open();
DbHelper.insertCarDetails(car_One);
DbHelper.close();
car_One = null;
DbHelper.open();
car_One = DbHelper.retriveCarDetails();
DbHelper.close();
ImageView carphoto = (ImageView) findViewById(R.id.photo);
carphoto.setImageBitmap(car_One.getBitmap());
}
}
我觉得这是一个小错误,因为我在使用getBitmap()
时也遇到错误“无法解析方法getBitmap()”尽管导入了它的库。
答案 0 :(得分:0)
检查您的Car类是否在myapplication
目录中以及此处提到的其他两个类。
然后从myapplication
目录编译。