2015-05-01 50 views
-1

我試圖讓我的數據庫可讀考慮,只有「categoria」是在選擇時被檢索,所以我開發這個代碼檢索數據:的Android數據庫SQLite的多哪來

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) 

你認爲是什麼導致錯誤?

回答

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); 

讓我知道這對你的作品。

+0

有趣的事情,我有那部分在每一個連續增加,但我刪除它CUS我雖然是我不需要的代碼的一部分了:P – NobodyNemo

+0

它工作感謝:D – NobodyNemo

+0

很好聽!當你忘記爲什麼存在一段代碼並刪除它時,創建多少次錯誤是很有趣的。 – anthonycr