简体   繁体   English

java.lang.IllegalArgumentException:Ormlite游标不存在列'_id'

[英]java.lang.IllegalArgumentException: column '_id' does not exist with Ormlite cursor

I'm using Ormlite with CursorLoader , using the ormlite-extras library (I use the package support.extras as I intend to target android 2.2 and up). 我正在使用Ormlite和CursorLoader ,使用ormlite-extras库(我使用包support.extras因为我打算将android 2.2及更高版本作为目标)。

I have the following exception : 我有以下例外:

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)

I did some digging and noticed that indeed '_id' is not present in the results returned by the code in the CursorLoader. 我做了一些挖掘,并注意到CursorLoader中的代码返回的results中确实没有'_id'

For more information, please see the code above : 有关详细信息,请参阅上面的代码:

/* 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;
}

Using the debugger, it appears the following : 使用调试器,它显示如下:

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}

The creation of the prepared query is pretty standard : 准备好的查询的创建非常标准:

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

I did the test executing the query and getting the results as a List : everything works alright. 我做了测试执行查询并将结果作为List:一切正常。

For now, I'm puzzled. 现在,我很困惑。

Besides, I have another for the ormlite experts. 此外,我还为ormlite专家提供了另一个。

  • Am I supposed to close the iterator by calling iterator.closeQuietly() ? 我应该通过调用iterator.closeQuietly()来关闭迭代iterator.closeQuietly()吗?

As pointed by @patheticpat, the solution is available on stackoverflow : ORMLite with CursorAdapter in Android 正如@patheticpat所指出的,解决方案可在stackoverflow上使用: ORMLite与Android中的CursorAdapter

Your id fields need to be mapped to a column named _id to use Cursor with Ormlite 4.42. 您的id字段需要映射到名为_id的列,以将Cursor与Ormlite 4.42一起使用。

Like this : 像这样 :

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

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

Note : And you can't call iterator.closeQuietly() because it crashes. 注意:你不能调用iterator.closeQuietly()因为它崩溃了。 I hope that there is no memory leaked because if that. 我希望没有内存泄露,因为如果那样的话。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 CursorAdapter java.lang.IllegalArgumentException:列&#39;_id&#39;不存在 - CursorAdapter java.lang.IllegalArgumentException: column '_id' does not exist java.lang.IllegalArgumentException:列&#39;_id&#39;不存在-UploadActivity - java.lang.IllegalArgumentException: column '_id' does not exist - UploadActivity Android SQLite java.lang.IllegalArgumentException:列&#39;_id&#39;不存在 - Android SQLite java.lang.IllegalArgumentException: column '_id' does not exist java.lang.IllegalArgumentException:列&#39;foo&#39;不存在android - java.lang.IllegalArgumentException: column 'foo' does not exist android Android SQLite首次使用:错误java.lang.IllegalArgumentException:列&#39;_id&#39;不存在 - Android SQLite first use: Error java.lang.IllegalArgumentException: column '_id' does not exist java.lang.IllegalArgumentException:列“状态”不存在 - java.lang.IllegalArgumentException: column 'status' does not exist java.lang.IllegalArgumentException:列“_data”不存在。 可用列:[] - java.lang.IllegalArgumentException: column '_data' does not exist. Available columns: [] java.lang.IllegalArgumentException:参数不作为命名参数存在 - java.lang.IllegalArgumentException: Parameter does not exist as a named parameter in android:java.lang.illegalargumentexception列&#39;details&#39;不存在 - android: java.lang.illegalargumentexception column 'details' does not exists java.lang.IllegalArgumentException:检索无效的图书 ID:1 - java.lang.IllegalArgumentException: Invalid retrievedBook Id:1
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM