我正在尝试使我的数据库可读,考虑到只检索了选中的“categoria”,所以我开发了这段代码:
plus = praia+ cafe+serv+rest+hot+muse+mon+escolas;
String dados[] = new String[plus];
//categoria is the name of the colum
String query = "categoria = ?";
for (i = 1; i < plus; i++) {
query = query+ " OR categoria = ?";
}
//if == 1 its active if == 0 its unactive;
if (praia == 1) {
dados[cont]= "Praias";
}
if (serv == 1) {
dados[cont]= "Servicos";
}
if (cafe == 1) {
dados[cont]= "Cafes";
}
if (rest == 1) {
dados[cont]= "Restaurantes";
}
if (hot == 1) {
dados[cont]= "Hoteis";
}
if (muse == 1) {
dados[cont]= "Museus";
}
if (mon == 1) {
dados[cont]= "Monumentos";
}
if (escolas == 1) {
dados[cont]= "Escolas";
}
String[] projections ={VianaContract.NewLocalInfo.Nome, VianaContract.NewLocalInfo.Categoria, VianaContract.NewLocalInfo.Latitude, VianaContract.NewLocalInfo.Longitude};
//error here
cursor = db.query(VianaContract.NewLocalInfo.Table_name, projections, query, dados, null, null, null);
这是logcat:
05-01 07:58:13.131 1435-1435/prosis.guiatour D/AndroidRuntime﹕ Shutting down VM
05-01 07:58:13.131 1435-1435/prosis.guiatour W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0xb3ac7ba8)
05-01 07:58:13.171 1435-1435/prosis.guiatour E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: prosis.guiatour, PID: 1435
java.lang.RuntimeException: Unable to start activity ComponentInfo{prosis.guiatour/prosis.guiatour.Lista}: java.lang.IllegalArgumentException: the bind value at index 3 is null
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.IllegalArgumentException: the bind value at index 3 is null
at android.database.sqlite.SQLiteProgram.bindString(SQLiteProgram.java:164)
at android.database.sqlite.SQLiteProgram.bindAllArgsAsStrings(SQLiteProgram.java:200)
at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:47)
at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1314)
at android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1161)
at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1032)
at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1200)
at prosis.guiatour.VianaDbHelper.getInfor(VianaDbHelper.java:123)
at prosis.guiatour.Lista.onCreate(Lista.java:49)
at android.app.Activity.performCreate(Activity.java:5231)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245) at android.app.ActivityThread.access$800(ActivityThread.java:135) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:136) at android.app.ActivityThread.main(ActivityThread.java:5017) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:515) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) at dalvik.system.NativeStart.main(Native Method)
您认为导致错误的原因是什么?
答案 0 :(得分:1)
问题在于,您正在为SQL数据库提供一个数组,以用作具有空绑定的条件。这意味着您的数组dados
具有空值,数据库会看到并抛出异常。这是因为您初始化dados
数组的方式。
查看您的代码,我看不到cont
初始化的位置,但我会假设您将其设置为零。现在我遇到的问题是你永远不会增加cont
,而是将它用作dados
数组中某个位置的每个赋值的索引。如您所见,这只是意味着您每次都覆盖相同的位置,因此假设所有这些值都是1,您将拥有一个大小为8的数组,但只有cont
的位置才会包含任何数据它和你的数组看起来像
{ Escolas, null, null, null, null, null, null, null }
到数据库,它必须抛出异常。
要解决此问题,只需按以下方式递增cont
:
plus = praia + cafe + serv + rest + hot + muse + mon + escolas;
String dados[] = new String[plus];
// categoria is the name of the column
String query = "categoria = ?";
for(i = 1; i < plus; i++){
query = query + " OR categoria = ?";
}
//if == 1 its active if == 0 its unactive;
cont = 0;
if(praia == 1){
dados[cont] = "Praias";
cont++;
}
if(serv == 1){
dados[cont] = "Servicos";
cont++;
}
if(cafe == 1){
dados[cont] = "Cafes";
cont++;
}
if(rest == 1){
dados[cont] = "Restaurantes";
cont++;
}
if(hot == 1){
dados[cont] = "Hoteis";
cont++;
}
if(muse == 1){
dados[cont] = "Museus";
cont++;
}
if(mon == 1){
dados[cont] = "Monumentos";
cont++;
}
if(escolas == 1){
dados[cont] = "Escolas";
cont++;
}
String[] projections ={VianaContract.NewLocalInfo.Nome, VianaContract.NewLocalInfo.Categoria, VianaContract.NewLocalInfo.Latitude, VianaContract.NewLocalInfo.Longitude};
cursor = db.query(VianaContract.NewLocalInfo.Table_name, projections, query, dados, null, null, null);
请告诉我这是否适合您。