0
我正在創建一個具有imageButton建築物的用戶可以購買的應用程序。購買時,將它們放到用戶佈局中,然後進行拖放。當用戶移動建築物時,座標在被放下時會自動保存。我的問題是,在加載函數來加載座標後,我得到了我在我的logcat中發佈的錯誤。我已經多次看到這個問題,並嘗試過所有我沒有看到過的運氣。請幫助傢伙。謝謝。添加代碼以從sqlite數據庫加載座標後,無法讀取行0,col -1從CursorWindow錯誤
這裏是我的列名:
public static final String KEY_ROWID = "_id";
public static final String KEY_ROCK = "amount_of_rock";
public static final String KEY_METEORROCK = "amount_of_meteor_rock";
public static final String KEY_WATER = "amount_of_water";
public static final String KEY_SHOVEL = "shovel_level";
public static final String KEY_SHOVELEXP = "shovel_experience";
public static final String KEY_BUCKET = "bucket_level";
public static final String KEY_BUCKETEXP = "bucket_experience";
public static final String KEY_PICKAXE = "pickaxe_level";
public static final String KEY_PICKAXEEXP = "pickaxe_experience";
public static final String KEY_POPULATION = "population";
public static final String KEY_MAINHUTLEVEL = "main_hut_level";
public static final String KEY_MAINHUTHEALTH = "main_hut_health";
public static final String KEY_MAINHUTX = "main_hut_x_coord";
public static final String KEY_MAINHUTY = "main_hut_y_ coord";
private static final String DATABASE_NAME = "2023db";
private static final String DATABASE_TABLE = "maintable";
private static final int DATABASE_VERSION = 1;
這裏是我得到mainHut(發生的錯誤上的getMainHutY功能旁邊有一個註釋行)的保存座標代碼:
public float getMainHutX()
{
float mainHutX = 0;
int iMainHutX = 0;
String[] columns = new String[] {KEY_ROWID, KEY_ROCK, KEY_METEORROCK, KEY_WATER,
KEY_POPULATION, KEY_SHOVEL, KEY_BUCKET, KEY_PICKAXE, KEY_SHOVELEXP,
KEY_BUCKETEXP, KEY_PICKAXEEXP, KEY_MAINHUTLEVEL, KEY_MAINHUTHEALTH,
KEY_MAINHUTX, KEY_MAINHUTY};
Cursor c = ourDatabase.query(DATABASE_TABLE, columns, null, null, null, null, null);
if (c != null && c.getCount() > 0)
{
if (c.moveToFirst())
{
iMainHutX = c.getColumnIndex(KEY_MAINHUTX);
mainHutX = c.getFloat(iMainHutX);
}//end if
c.close();
return mainHutX;
}//end if
return 0;
}//end getMainHutX function
public float getMainHutY()
{
float mainHutY = 0;
int iMainHutY = 0;
String[] columns = new String[] {KEY_ROWID, KEY_ROCK, KEY_METEORROCK, KEY_WATER,
KEY_POPULATION, KEY_SHOVEL, KEY_BUCKET, KEY_PICKAXE, KEY_SHOVELEXP,
KEY_BUCKETEXP, KEY_PICKAXEEXP, KEY_MAINHUTLEVEL, KEY_MAINHUTHEALTH,
KEY_MAINHUTX, KEY_MAINHUTY};
Cursor c = ourDatabase.query(DATABASE_TABLE, columns, null, null, null, null, null);
if (c != null && c.getCount() > 0)
{
if (c.moveToFirst())
{
iMainHutY = c.getColumnIndex(KEY_MAINHUTY);
mainHutY = c.getFloat(iMainHutY); //error occurs here
}//end if
c.close();
return mainHutY;
}//end if
return 0;
}//end getMainHutY function
這裏是我的logcat:
10-02 22:32:18.036: E/CursorWindow(561): Failed to read row 0, column -1 from a CursorWindow which has 1 rows, 15 columns.
10-02 22:32:18.036: E/AndroidRuntime(561): FATAL EXCEPTION: main
10-02 22:32:18.036: E/AndroidRuntime(561): Process: com.twentytwentythree.sab, PID: 561
10-02 22:32:18.036: E/AndroidRuntime(561): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.twentytwentythree.sab/com.project.llb.runGraphics}: java.lang.IllegalStateException: Couldn't read row 0, col -1 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it.
10-02 22:32:18.036: E/AndroidRuntime(561): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2596)
10-02 22:32:18.036: E/AndroidRuntime(561): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2653)
10-02 22:32:18.036: E/AndroidRuntime(561): at android.app.ActivityThread.access$800(ActivityThread.java:156)
10-02 22:32:18.036: E/AndroidRuntime(561): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1355)
10-02 22:32:18.036: E/AndroidRuntime(561): at android.os.Handler.dispatchMessage(Handler.java:102)
10-02 22:32:18.036: E/AndroidRuntime(561): at android.os.Looper.loop(Looper.java:157)
10-02 22:32:18.036: E/AndroidRuntime(561): at android.app.ActivityThread.main(ActivityThread.java:5872)
10-02 22:32:18.036: E/AndroidRuntime(561): at java.lang.reflect.Method.invoke(Native Method)
10-02 22:32:18.036: E/AndroidRuntime(561): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:852)
10-02 22:32:18.036: E/AndroidRuntime(561): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:668)
10-02 22:32:18.036: E/AndroidRuntime(561): Caused by: java.lang.IllegalStateException: Couldn't read row 0, col -1 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it.
10-02 22:32:18.036: E/AndroidRuntime(561): at android.database.CursorWindow.nativeGetDouble(Native Method)
10-02 22:32:18.036: E/AndroidRuntime(561): at android.database.CursorWindow.getDouble(CursorWindow.java:597)
10-02 22:32:18.036: E/AndroidRuntime(561): at android.database.CursorWindow.getFloat(CursorWindow.java:648)
10-02 22:32:18.036: E/AndroidRuntime(561): at android.database.AbstractWindowedCursor.getFloat(AbstractWindowedCursor.java:81)
10-02 22:32:18.036: E/AndroidRuntime(561): at com.twentytwentythree.sab.Database.getMainHutY(Database.java:228)
10-02 22:32:18.036: E/AndroidRuntime(561): at com.twentytwentythree.sab.runGraphics.onCreate(runGraphics.java:142)
10-02 22:32:18.036: E/AndroidRuntime(561): at android.app.Activity.performCreate(Activity.java:5312)
10-02 22:32:18.036: E/AndroidRuntime(561): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1111)
10-02 22:32:18.036: E/AndroidRuntime(561): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2552)
10-02 22:32:18.036: E/AndroidRuntime(561): ... 9 more
抱歉。我添加了列名稱聲明。 – sboehnke 2014-10-03 04:18:38
這讓我感覺真的很愚蠢......但是當我發佈聲明時,我意識到問題是......在聲明我遇到問題之後,我在字符串中留下了一個空格。謝謝,沒有這個我就不會看到它。 – sboehnke 2014-10-03 04:22:34
太棒了!很高興我能幫上忙。 – 2014-10-03 04:30:23