我試圖在一個expandableListView中獲取一個子級的位置id,並根據點擊的子級啓動另一個類,並且正在執行此操作像這樣:CursorIndexOutOfBoundsException:請求索引0,大小爲0.錯誤 - Android
@Override
public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id)
{
// TODO Auto-generated method stub
super.onChildClick(parent, v, groupPosition, childPosition, id);
Intent i = new Intent(this, SavedEntries.class);
i.putExtra("_id", id);
startActivityForResult(i, ACTIVITY_EDIT);
return super.onChildClick(parent, v, groupPosition, childPosition, id);
}
但我不斷收到此錯誤:
01-05 13:14:28.232: E/AndroidRuntime(316): FATAL EXCEPTION: main
01-05 13:14:28.232: E/AndroidRuntime(316): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.x17.projects.strikezone/com.x17.projects.strikezone.SavedEntries}: android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
01-05 13:14:28.232: E/AndroidRuntime(316): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
01-05 13:14:28.232: E/AndroidRuntime(316): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
01-05 13:14:28.232: E/AndroidRuntime(316): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
01-05 13:14:28.232: E/AndroidRuntime(316): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
01-05 13:14:28.232: E/AndroidRuntime(316): at android.os.Handler.dispatchMessage(Handler.java:99)
01-05 13:14:28.232: E/AndroidRuntime(316): at android.os.Looper.loop(Looper.java:123)
01-05 13:14:28.232: E/AndroidRuntime(316): at android.app.ActivityThread.main(ActivityThread.java:4627)
01-05 13:14:28.232: E/AndroidRuntime(316): at java.lang.reflect.Method.invokeNative(Native Method)
01-05 13:14:28.232: E/AndroidRuntime(316): at java.lang.reflect.Method.invoke(Method.java:521)
01-05 13:14:28.232: E/AndroidRuntime(316): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
01-05 13:14:28.232: E/AndroidRuntime(316): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
01-05 13:14:28.232: E/AndroidRuntime(316): at dalvik.system.NativeStart.main(Native Method)
01-05 13:14:28.232: E/AndroidRuntime(316): Caused by: android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
01-05 13:14:28.232: E/AndroidRuntime(316): at android.database.AbstractCursor.checkPosition(AbstractCursor.java:580)
01-05 13:14:28.232: E/AndroidRuntime(316): at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:214)
01-05 13:14:28.232: E/AndroidRuntime(316): at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:41)
01-05 13:14:28.232: E/AndroidRuntime(316): at com.x17.projects.strikezone.SavedEntries.getDateRow(SavedEntries.java:56)
01-05 13:14:28.232: E/AndroidRuntime(316): at com.x17.projects.strikezone.SavedEntries.onCreate(SavedEntries.java:48)
01-05 13:14:28.232: E/AndroidRuntime(316): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
01-05 13:14:28.232: E/AndroidRuntime(316): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
01-05 13:14:28.232: E/AndroidRuntime(316): ... 11 more
這是我送的ID來:
Bundle extras = getIntent().getExtras();
mRowId = extras != null ? extras.getLong("_id") : null;
private void getDateRow()
{
Cursor mCursor = mDbHelper.fetchRow(mRowId);
date = mCursor.getString(0); //Line 56, this is where the error occurs.
mCursor.close();
}
下面是對上面的查詢:
public Cursor fetchRow(long rowId)
{
Cursor cursor = mDb.rawQuery("SELECT date FROM entry WHERE _id=" + rowId, null);
if(cursor != null)
{
cursor.moveToFirst();
}
return cursor;
}
爲什麼會出現這個錯誤?我在這裏做錯了什麼?
在此先感謝。
是的,日期字段是一個字符串。我檢查了光標計數,它是0,所以這就是發生這種錯誤的原因。我怎麼能從被點擊的孩子身上獲得ID並將其傳遞給SavedEntries.class? – user990230 2012-01-05 19:12:22
調試並查看id是否在onChildClick方法中獲得了所需的值。 – kosa 2012-01-05 19:19:22
由於某種原因,當onChildClick被調用時,groupPosition,childPosition和id都會得到一個0值。 – user990230 2012-01-05 19:34:24