我正在尝试将记录插入sqlite数据库并绕过contentResolver。名为EventProvider的第一个类扩展了ContentProvider,还包含我的SQLite数据库助手类的实例,如下所示。因此,当我通过contentResolver插入记录时,第一个类中的代码工作正常,但在EventJSONParser类中我想直接插入记录sqlite,而不是通过SQLiteDatabase db = provider.getDatabase()中的NullPointerException; unmarshalEventQuery方法中的代码行。我怀疑onCreate()方法不会创建databasehelper的实例。我需要有关如何解决这个问题的建议。感谢。
头等舱:
public class EventProvider extends ContentProvider{
private DatabaseHelper helper;
private SQLiteDatabase db;
@Override
public boolean onCreate() {
helper = new DatabaseHelper(getContext());
db = helper.getWritableDatabase();
return true;
}
public SQLiteDatabase getDatabase(){
return db;
}
@Override
public Uri insert(Uri uri, ContentValues values) {
if (BuildConfig.DEBUG) { Log.d(TAG, "insert@" + uri); }
switch (uriMatcher.match(uri)){
case EVENTS_DIR:
break;
default:
throw new UnsupportedOperationException("Unrecognized URI: " + uri);
}
values = COLUMN_MAP.translateCols(values);
values.put(DatabaseContract.COL_DIRTY, MARK);enter code here
SQLiteDatabase db = getDatabase();
return eventInsert(uri, values, db);
}
public Uri eventInsert(Uri uri, ContentValues values, SQLiteDatabase mdb){
if (BuildConfig.DEBUG){
Log.d(TAG, "insert@" + uri + ": {" + values + "}");
}
long pk = mdb.insert(DatabaseContract.EVENT_TABLE, null, values);
if (pk > 0) {
uri = null;
} else{
uri = uri.buildUpon().appendPath(String.valueOf(pk)).build();
getContext().getContentResolver().notifyChange(uri, null);
}
return uri;
}
第二课:
public class EventJSONParser {
private EventProvider provider;
public EventJSONParser(EventProvider restfulProvider){
provider = restfulProvider;
}
public void unmarshalEventQuery(JSONObject json){
try {
String error = json.getString(Const.KEY_ERROR);
int statusCode = json.getInt(Const.KEY_STATUS_CODE);
if (error.equals("false") && statusCode == 200){
JSONArray events = json.getJSONArray(TAG_EVENTS);
for(int i = 0; i < events.length(); i++){
JSONObject e = events.getJSONObject(i);
ContentValues values = new ContentValues();
values.put(UNMARSHAL_TAB.get(TAG_EVENT_ID), e.getString(TAG_EVENT_ID));
values.put(UNMARSHAL_TAB.get(TAG_CAL_ID), e.getString(TAG_CAL_ID));
values.put(UNMARSHAL_TAB.get(TAG_EVENT_TITLE), e.getString(TAG_EVENT_TITLE));
values.put(UNMARSHAL_TAB.get(TAG_START_DATE), e.getString(TAG_START_DATE));
values.put(UNMARSHAL_TAB.get(TAG_END_DATE), e.getString(TAG_END_DATE));
values.put(UNMARSHAL_TAB.get(TAG_LOCATION), e.getString(TAG_LOCATION));
values.put(UNMARSHAL_TAB.get(TAG_VISIBILITY), e.getString(TAG_VISIBILITY));
values.put(UNMARSHAL_TAB.get(TAG_DESCRIPTION), e.getString(TAG_DESCRIPTION));
SQLiteDatabase db = provider.getDatabase();
Uri uri = provider.eventInsert(EventContract.EVENT_URI, values, db);
Log.e(TAG, "query uri" + uri);
}
}
}catch (JSONException ex){
ex.printStackTrace();
}
}
}
答案 0 :(得分:0)
在将操作运行到数据库之前,您应该打开数据库。 此类文件link
public CourseGateWay(Context context) {
dbOpenHelper = new DBOpenHelper(context);
this.context = context;
}
public void open() {
try {
sqLiteDB = dbOpenHelper.getWritableDatabase();
} catch (SQLException s) {
new Exception("Error with DB Open");
}
}
public void close() {
sqLiteDB.close();
}
public String saveCourse(Courses aCore) {
ContentValues contentValues = new ContentValues();
open();
contentValues.put(DBOpenHelper.COURS_CODE, aCore.getcCode());
contentValues.put(DBOpenHelper.COURS_NAME, aCore.getcName());
contentValues.put(DBOpenHelper.COURS_DEPT, aCore.getdCode());
long res = sqLiteDB.insert(DBOpenHelper.TABLE_COURS, null,
contentValues);
close();
if (res > 0) {
return "Course Added with: " + aCore.getcCode();
} else {
return "Duplicate Entry with: " + aCore.getcName();
}
}
请参阅我的github:here
为了更好地在Manifesto文件中授予权限:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />