我说的是运行时错误
“ java.lang.NullPointerException:尝试在空对象引用”
上调用虚拟方法'android.database.sqlite.SQLiteDatabase com.example.android.pets.data.petsdbhelper.getReadableDatabase()'但是我初始化了它!我该如何解决呢?
public class petProvider extends ContentProvider {
public static final String LOG_TAG = petProvider.class.getSimpleName();
public petsdbhelper phelper = new petsdbhelper(getContext());
public final static UriMatcher uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
static {
uriMatcher.addURI(blankContract.content_authority,"pets",1);
uriMatcher.addURI(blankContract.content_authority,"pets/#",2);
}
@Override
public boolean onCreate() {
return true;
}
@Nullable
@Override
public Cursor query(@NonNull Uri uri, @Nullable String[] strings, @Nullable String s, @Nullable String[] strings1, @Nullable String s1) {
SQLiteDatabase sql = phelper.getReadableDatabase();
Cursor cursor = null ;
int match = uriMatcher.match(uri);
switch (match){
case 1:
cursor = sql.query(blankContract.blankEntry.TABLE_NAME , strings,s,strings1,null,null,s1);
break;
case 2:
s = blankContract.blankEntry.COULUMN_ID +"=?";
strings1 = new String[] { String.valueOf(ContentUris.parseId(uri))};
cursor = sql.query(blankContract.blankEntry.TABLE_NAME,strings,s,strings1,null,null,s1);
break;
default:
throw new IllegalArgumentException("cannot query unknown "+uri);
}
return cursor;
}