Since you call a static method from a class that extends Activity
, the onCreate
method of that class is obviously not called, that's why you are getting NPE.
Firstly move all your methods in the utility class to your MovieDbHelper class, and use the context
provided in the constructor.
public class MovieDbHelper extends SQLiteOpenHelper {
private Context context;
private static final int DATABASE_VERSION = 2;
static final String DATABASE_NAME = "movie.db";
public MovieDbHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
this.context = context;
}
}
for example your getEntry
(put it in the MovieDbHelper
) can be rewritten as:
public Cursor getEntry(String[] columns){
SQLiteDatabase db = this.getReadableDatabase();
return db.query("MOVIE",columns,null,null,null,null,null); **-- nullpointer**
}
EDITED:
usage in your activity:
MovieDbHelper helper = new MovieDbHelper(YourActivityName.this);
String[] cm = {"mov_pos"};
Cursor c = helper.getEntry(cm);
usage in your fragment:
MovieDbHelper helper = new MovieDbHelper(getActivity().getApplicationContext());
String[] cm = {"mov_pos"};
Cursor c = helper.getEntry(cm);
EDIT 2:
public addEntry( byte[] image) throws SQLiteException {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put("mov_pos", image);
try {
db.insertOrThrow("MOVIE", null, cv );
} catch (SQLiteConstraintException e) {
e.printStackTrace();
return false;
}
return true;
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…