我编写了自定义contentprovider,它使用单个表'user_authentication'在sqlite3中创建数据库。android自定义内容提供者
尽管已在其各自的onCreate()
方法上创建了数据库和表,但我在覆盖插入方法中获得了NullPointerException
。下面是方法:
public Uri insert(Uri uri, ContentValues initialvalues){
SQLiteDatabase sqlitedb = dbHelper.getWritableDatabase();
if(sUriMatcher.match(uri)!= TABLE){
throw new IllegalArgumentException("Unkown Uri"+uri);
}
ContentValues values;
if(initialvalues != null){
values = new ContentValues(initialvalues);
}
else{
values = new ContentValues();
}
long rowId = sqlitedb.insert(AUTHENTICATION_TABLE_NAME, "", values);
if(rowId>0){
Uri uril = ContentUris.withAppendedId(User.CONTENT_URI, rowId);
getContext().getContentResolver().notifyChange(User.CONTENT_URI, null);
sqlitedb.close();
return uril;
}
throw new SQLException("Failed to insert row into User_Authentication : " + uri);
}
调试时,我看到桌子被插入与“价值”,但是当它到达getContext()
它返回null,所以错误。
我想知道为什么getContext()
returing空,因为自定义contentprovider类扩展android.content.ContentProvider
;
任何人都可以请帮助我这个。顺便说一句,我使用的2.3 SDK
感谢, 拉贾钱德拉Rangineni
这是调用onCreate()后调用? – MByD 2011-03-23 22:28:04