我正在尝试使用内容提供商,但我有错误:
类型MCProvider中的方法查询(Uri,String [],String,String [],String)不适用于参数(Uri,String [],String [],String [],String [])
我的代码:
public class MapaLugares extends FragmentActivity {
SQLiteDatabase db;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mapa_lugares);
final MCProvider Provc = new MCProvider();
final String AUT = "com.app.mapa.MCProvider";
final Uri C_URI = Uri.parse("content://"+AUT+Bdoh.T_NOMBRE);
final String[] p = new String[] {com.app.mapa.MCProvider.latitud,
com.app.mapa.MCProvider.longitud};
Cursor crs;
mMap = ((SupportMapFragment)
getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
mMap.setOnMapClickListener(new OnMapClickListener() {
@Override
public void onMapClick(LatLng point) {
Projection proj = mMap.getProjection();
Point coord = proj.toScreenLocation(point);
final String[] s = new String[]
{com.app.mapa.MCProvider.latitud + " = '" + point.latitude +
"AND" + com.app.mapa.MCProvider.longitud + " = '" +
point.longitude};
final String[] sarg=null;
final String[] sorder=null;
Provc.query(C_URI, p, s, sarg, sorder);
}
});
}
内容提供商:
public class MCProvider extends ContentProvider {
private static final String AUTH = "com.app.mapa.MCProvider";
private static final Uri CP_URI = Uri.parse("content://"+AUTH+Bdoh.T_NOMBRE);
private static int COMMENT = 1;
public static String latitud;
public static String longitud;
SQLiteDatabase bd;
Bdoh bdHelper;
private static final UriMatcher Umatcher;
static
{
Umatcher = new UriMatcher(UriMatcher.NO_MATCH);
Umatcher.addURI(AUTH, Bdoh.T_NOMBRE, COMMENT);
}
@Override
public boolean onCreate()
{
// TODO Auto-generated method stub
bdHelper = new Bdoh(getContext());
return true;
}
@Override
public Uri insert(Uri uri, ContentValues cvalues)
{
// TODO Auto-generated method stub
bd = bdHelper.getWritableDatabase();
if(Umatcher.match(uri)==COMMENT)
{
bd.insert(Bdoh.T_NOMBRE, null, cvalues);
}
bd.close();
getContext().getContentResolver().notifyChange(uri, null);
return null;
}
@Override
public Cursor query(Uri uri, String[] projection, String selection, String[]
selectionargs, String sortorder)
{
// TODO Auto-generated method stub
Cursor cr;
bd = bdHelper.getReadableDatabase();
//bd.query
cr = bd.query(Bdoh.T_NOMBRE, projection, selection, selectionargs, null,
null, sortorder);
cr.setNotificationUri(getContext().getContentResolver(), uri);
return cr;
}
@Override
public int delete(Uri arg0, String arg1, String[] arg2) {
// TODO Auto-generated method stub
return 0;
}
@Override
public String getType(Uri arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public int update(Uri arg0, ContentValues arg1, String arg2, String[] arg3) {
// TODO Auto-generated method stub
return 0;
}
}
SQLiteOpenHelper类:
public class Bdoh extends SQLiteOpenHelper
{
String bdcons = "CREATE TABLE lugares (_id INTEGER PRIMARY KEY AUTOINCREMENT,
nombre TEXT, descripcion TEXT, latitud DOUBLE, longitud DOUBLE, foto TEXT)";
SQLiteOpenHelper sbd;
static String T_NOMBRE="lugares"; //para el content provider
Cursor cr;
String TBD="lugares"; //tabla de la base de datos
public Bdoh(Context context)
{
super(context, "lugares", null, 1);
SQLiteDatabase
db=context.openOrCreateDatabase("lugares",Context.MODE_PRIVATE,null);
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase db)
{
// TODO Auto-generated method stub
db.execSQL(bdcons);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
// TODO Auto-generated method stub
db.execSQL("DROP TABLE IF EXISTS lugares");
db.execSQL(bdcons);
}
public void insertar(SQLiteDatabase db)
{
db=sbd.getWritableDatabase();
cr=db.rawQuery("INSERT INTO lugares ", null);
}
}
为什么会出现这个错误?
感谢。
答案 0 :(得分:0)
错误已修复。问题出在MapaLugares类中的onMapClick事件中,除了sarg变量之外,字符串的声明没有[]。