gpt4 book ai didi

java.lang.IllegalArgumentException : column '_id' does not exist with Ormlite cursor 异常

转载 作者:行者123 更新时间:2023-11-29 05:50:37 24 4
gpt4 key购买 nike

我将 Ormlite 与 CursorLoader 一起使用,使用 ormlite-extras库(我使用包 support.extras 因为我打算针对 android 2.2 及更高版本)。

我有以下异常:

FATAL EXCEPTION: main
java.lang.IllegalArgumentException: column '_id' does not exist
at android.database.AbstractCursor.getColumnIndexOrThrow(AbstractCursor.java:314)
at android.support.v4.widget.CursorAdapter.swapCursor(CursorAdapter.java:344)
at rainstudios.meleo.ui.eventlv.EventLvFragment.onLoadFinished(EventLvFragment.java:274)
at rainstudios.meleo.ui.eventlv.EventLvFragment.onLoadFinished(EventLvFragment.java:1)
at android.support.v4.app.LoaderManagerImpl$LoaderInfo.callOnLoadFinished(LoaderManager.java:427)
at android.support.v4.app.LoaderManagerImpl$LoaderInfo.onLoadComplete(LoaderManager.java:395)
at android.support.v4.content.Loader.deliverResult(Loader.java:103)
at rainstudios.meleo.data.RainstudiosOrmliteCursorLoader.deliverResult(RainstudiosOrmliteCursorLoader.java:68)
at rainstudios.meleo.data.RainstudiosOrmliteCursorLoader.deliverResult(RainstudiosOrmliteCursorLoader.java:1)
at android.support.v4.content.AsyncTaskLoader.dispatchOnLoadComplete(AsyncTaskLoader.java:221)
at android.support.v4.content.AsyncTaskLoader$LoadTask.onPostExecute(AsyncTaskLoader.java:61)
at android.support.v4.content.ModernAsyncTask.finish(ModernAsyncTask.java:461)
at android.support.v4.content.ModernAsyncTask.access$500(ModernAsyncTask.java:47)
at android.support.v4.content.ModernAsyncTask$InternalHandler.handleMessage(ModernAsyncTask.java:474)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:3835)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:847)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:605)
at dalvik.system.NativeStart.main(Native Method)

我做了一些挖掘,发现 CursorLoader 中的代码返回的 results 中确实没有 '_id'

更多信息,请看上面的代码:

/* Runs on a worker thread */
@Override
public Cursor loadInBackground() {
Cursor cursor = null;
try {
CloseableIterator<T> iterator = mDao.iterator(mQuery);

// get the raw results which can be cast under Android
AndroidDatabaseResults results = (AndroidDatabaseResults) iterator
.getRawResults();
cursor = results.getRawCursor();
} catch (SQLException e) {
e.printStackTrace();
}
if (cursor != null) {
// Ensure the cursor window is filled
cursor.getCount();
registerContentObserver(cursor, mObserver);
}
return cursor;
}

使用调试器,出现如下:

results.columnNameMap = [id, code, name, popular, rating, voteCount, shortDescription, description, hoursDescription, url, modificationDate, syncDate, artist_id, district_id, pictures, frontPicture, userRating, venue_id, userComment, userFavorite, situation_id, caFrontPictureId, caResources]

results.columnNames = {syncDate=11, userRating=16, district_id=13, pictures=14, userFavorite=19, artist_id=12, code=1, situation_id=20, frontPicture=15, voteCount=5, url=9, caResources=22, id=0, hoursDescription=8, userComment=18, shortDescription=6, description=7, popular=3, name=2, venue_id=17, rating=4, caFrontPictureId=21, modificationDate=10}

准备好的查询的创建非常标准:

public PreparedQuery<EventDb> findAllEventsQuery() {
try {

QueryBuilder<EventDb, Integer> eventQb = getEventDao()
.queryBuilder();
return eventQb.prepare();

} catch (Throwable e) {
Out.handleSilentError("Cannot retrieve all events", e);
}
return null;
}

我进行了测试,执行查询并将结果作为列表获取:一切正常。

现在,我很困惑。

此外,我还有一个给 ormlite 专家的。

  • 我是否应该通过调用 iterator.closeQuietly() 来关闭迭代器?

最佳答案

正如@patheticpat 所指出的,解决方案可在 stackoverflow 上找到:ORMLite with CursorAdapter in Android

您的 ID 字段需要映射到名为 _id 的列,以便在 Ormlite 4.42 中使用 Cursor。

像这样:

@DatabaseTable(tableName = "event")
public class EventDb extends BaseDaoEnabled<EventDb, Integer> {

@DatabaseField(generatedId = true, columnName = "_id")
int id;

注意:您不能调用 iterator.closeQuietly() 因为它会崩溃。我希望没有内存泄漏,因为如果那样的话。

关于java.lang.IllegalArgumentException : column '_id' does not exist with Ormlite cursor 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14006192/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com