在继承了Activity的类里面调用cursor =this.getContentResolver().query报空指针错误


在执行query这个方法的时候,实际上面是根据配置文件中的:

             android:name=".PersonContentProvider"

             android:authorities="com.gao.provider.PersonContentProvider" />

       

的调用PersonContentProvider类里面写的query方法。如果这个方法里面不返回cursor的话,那么就会报空指针。

比如我的文件开始是这样写的:

public Cursor query(Uri uri, String[] projection, String selection,
			String[] selectionArgs, String sortOrder) {
		SQLiteDatabase database=dataBaseOpenHelper.getReadableDatabase();
		switch (uriMatcher.match(uri)) {
		case ALLPERSON:
			database.query("person", projection, selection, selectionArgs, null, null, sortOrder);
			break;
		case PERSON:
			long id=ContentUris.parseId(uri);
			String where=TextUtils.isEmpty(selection)?"personid=?":selection+"and personid=?";
			String[] params=new String[]{String.valueOf(id)};
			if (!TextUtils.isEmpty(selection)&&selectionArgs!=null) {
				params=new String[selectionArgs.length+1];
				for (int i = 0; i < selectionArgs.length; i++) {
					params[i]=selectionArgs[i];
				}
				params[selectionArgs.length+1]=String.valueOf(id);
			}
			database.query("person", projection, where, params, null, null, sortOrder);
			break;
		default:
			break;
		}
		return null;
	}


这样就会报空指针异常

 

public Cursor query(Uri uri, String[] projection, String selection,String[] selectionArgs, String sortOrder) {
		SQLiteDatabase database=dataBaseOpenHelper.getReadableDatabase();
		Cursor cursor = null;
		switch (uriMatcher.match(uri)) {
		case ALLPERSON:
			cursor=database.query("person", projection, selection, selectionArgs, null, null, sortOrder);
			break;
		case PERSON:
			long id=ContentUris.parseId(uri);
			String where=TextUtils.isEmpty(selection)?"personid=?":selection+"and personid=?";
			String[] params=new String[]{String.valueOf(id)};
			if (!TextUtils.isEmpty(selection)&&selectionArgs!=null) {
				params=new String[selectionArgs.length+1];
				for (int i = 0; i < selectionArgs.length; i++) {
					params[i]=selectionArgs[i];
				}
				params[selectionArgs.length+1]=String.valueOf(id);
			}
			cursor=database.query("person", projection, where, params, null, null, sortOrder);
			break;
		default:
			break;
		}
		return cursor;
	}

代码下载:http://download.csdn.net/detail/u012242610/6941977

在继承了Activity的类里面调用cursor =this.getContentResolver().query报空指针错误_第1张图片

你可能感兴趣的:(在继承了Activity的类里面调用cursor =this.getContentResolver().query报空指针错误)