我正在尝试使用sqlite DB在我的Android应用程序中存储图像以及其他信息,但我收到此错误
06-20 12:00:51.411 4132-4132/braindottech.com.fishid E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: braindottech.com.fishid, PID: 4132
java.lang.NullPointerException: Attempt to invoke virtual method 'android.database.sqlite.SQLiteDatabase android.content.Context.openOrCreateDatabase(java.lang.String, int, android.database.sqlite.SQLiteDatabase$CursorFactory, android.database.DatabaseErrorHandler)' on a null object reference
at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:223)
at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:163)
at braindottech.com.fishid.PhotoDBMS.<init>(PhotoDBMS.java:65)
at braindottech.com.fishid.CameraScan$6.onClick(UseSqliteDB.java:23)
at android.view.View.performClick(View.java:5204)
at android.view.View$PerformClick.run(View.java:21153)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
这是我的&#34; UseSqliteDB.java&#34;代码:
public class UseSqliteDB extends Fragment {
private Button saveResult;
public UseSqliteDB(){}
@Override
public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); }
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, final Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_camera, container, false);
context = this.getActivity();
saveResult = (Button) v.findViewById(R.id.button_save_CameraPreview);
saveResult.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
try {
PhotoDBMS photoDBMS = new PhotoDBMS(getActivity());
String nameImage = "IMAGE";
Calendar calendar = Calendar.getInstance();
nameImage += calendar.get(Calendar.DATE) + calendar.get(Calendar.HOUR) + calendar.get(Calendar.MINUTE) +
calendar.get(Calendar.SECOND) + calendar.get(Calendar.MILLISECOND);
Boolean response = photoDBMS.insertData(nameImage, resultTV.getText().toString(), finalBitmap);
if (!response) {
Toast.makeText(context, "Database entry failed!", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(context, "Database entry successful!", Toast.LENGTH_LONG).show();
}
}catch(Exception e){
Log.d("HIP","E = "+e);
}
}
});
return v;
}
}
PhotoDBMS.java
public class PhotoDBMS {
private Context context;
public PhotoDBMS(Context c){
context = c;
}
public static abstract class DBMS_Constants implements BaseColumns {
public static final String TABLE_NAME = "MY_COLLECTION";
public static final String COLUMN_NAME_NAME = "Name";
public static final String COLUMN_NAME_INFO = "Info";
public static final String COLUMN_NAME_IMAGE = "Image";
}
private static final String TEXT_TYPE = " TEXT";
private static final String COMMA_SEP = ",";
private static final String BLOB = "BLOB";
private static final String SQL_CREATE_TABLE = "CREATE TABLE " + DBMS_Constants.TABLE_NAME + " (" +
DBMS_Constants._ID + " INTEGER PRIMARY KEY," +
DBMS_Constants.COLUMN_NAME_NAME + TEXT_TYPE + COMMA_SEP +
DBMS_Constants.COLUMN_NAME_INFO + TEXT_TYPE + COMMA_SEP +
DBMS_Constants.COLUMN_NAME_IMAGE + BLOB + " )";
private static final String SQL_DELETE_ENTRIES = "DROP TABLE IF EXISTS " + DBMS_Constants.TABLE_NAME;
public class DbHelper extends SQLiteOpenHelper {
// If you change the database schema, you must increment the database version.
public static final int DATABASE_VERSION = 1;
public static final String DATABASE_NAME = "FeedReader.db";
public DbHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
public void onCreate(SQLiteDatabase db) {
db.execSQL(SQL_CREATE_TABLE);
}
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// This database is only a cache for online data, so its upgrade policy is
// to simply to discard the data and start over
db.execSQL(SQL_DELETE_ENTRIES);
onCreate(db);
}
public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
onUpgrade(db, oldVersion, newVersion);
}
}
DbHelper dbHelper = new DbHelper(context);
SQLiteDatabase collectionDB = dbHelper.getWritableDatabase();
public boolean insertData(String name, String info, Bitmap bm){
bitmapUtility utility = new bitmapUtility();
byte[] image = utility.getBytes(bm);
ContentValues cv = new ContentValues();
cv.put(DBMS_Constants.COLUMN_NAME_NAME, name);
cv.put(DBMS_Constants.COLUMN_NAME_INFO, info);
cv.put(DBMS_Constants.COLUMN_NAME_IMAGE, image);
return collectionDB.insert( DBMS_Constants.TABLE_NAME, null, cv ) > 0;
}
public boolean deleteData(String name){
return collectionDB.delete(DBMS_Constants.TABLE_NAME, DBMS_Constants.COLUMN_NAME_NAME + " = " + name, null) > 0;
}
}
我在UseSqliteDB.java中的try-catch部分遇到此错误。请帮我解决这个错误。提前谢谢!
答案 0 :(得分:3)
问题是由于:
DbHelper dbHelper = new DbHelper(context);
SQLiteDatabase collectionDB = dbHelper.getWritableDatabase();
在类级而不是在任何方法中添加行。
创建一个方法并使用photoDBMS
调用它来创建和获取数据库对象:
public void createDB(){
DbHelper dbHelper = new DbHelper(context);
SQLiteDatabase collectionDB = dbHelper.getWritableDatabase();
}
并在Button onClick方法中调用它:
PhotoDBMS photoDBMS = new PhotoDBMS(getActivity());
photoDBMS.createDB();
或者在PhotoDBMS
类构造函数
答案 1 :(得分:1)
试试这个,
将您的PhotoDBMS
课程改为
public class DbHelper extends SQLiteOpenHelper {
public static final String TABLE_NAME = "MY_COLLECTION";
public static final String COLUMN_NAME_NAME = "Name";
public static final String COLUMN_NAME_INFO = "Info";
public static final String COLUMN_NAME_IMAGE = "Image";
private static final String _ID = "id";
private static final String TEXT_TYPE = " TEXT";
private static final String COMMA_SEP = ",";
private static final String BLOB = "BLOB";
private static final String SQL_CREATE_TABLE = "CREATE TABLE " + TABLE_NAME + " (" +
_ID + " INTEGER PRIMARY KEY," +
COLUMN_NAME_NAME + TEXT_TYPE + COMMA_SEP +
COLUMN_NAME_INFO + TEXT_TYPE + COMMA_SEP +
COLUMN_NAME_IMAGE + BLOB + " )";
private static final String SQL_DELETE_ENTRIES = "DROP TABLE IF EXISTS " + TABLE_NAME;
// If you change the database schema, you must increment the database version.
public static final int DATABASE_VERSION = 1;
public static final String DATABASE_NAME = "FeedReader.db";
public DbHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
public void onCreate(SQLiteDatabase db) {
db.execSQL(SQL_CREATE_TABLE);
}
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// This database is only a cache for online data, so its upgrade policy is
// to simply to discard the data and start over
db.execSQL(SQL_DELETE_ENTRIES);
onCreate(db);
}
public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
onUpgrade(db, oldVersion, newVersion);
}
public boolean insertData(String name, String info, Bitmap bm){
SQLiteDatabase sqLiteDatabase = getWritableDatabase();
bitmapUtility utility = new bitmapUtility();
byte[] image = utility.getBytes(bm);
ContentValues cv = new ContentValues();
cv.put(COLUMN_NAME_NAME, name);
cv.put(COLUMN_NAME_INFO, info);
cv.put(COLUMN_NAME_IMAGE, image);
return sqLiteDatabase.insert( TABLE_NAME, null, cv ) > 0;
}
public boolean deleteData(String name){
SQLiteDatabase sqLiteDatabase = getWritableDatabase();
return sqLiteDatabase.delete(TABLE_NAME, COLUMN_NAME_NAME + " = " + name, null) > 0;
}
}
call
DbHelper dbHelper = new DbHelper(getActivity());
dbHelper.insertData(nameImage, resultTV.getText().toString(), finalBitmap);