带有 simplecursor 适配器的 Android 列表视图会使应用程序崩溃(未显示 ANR)

发布于 2024-12-02 13:07:47 字数 5878 浏览 3 评论 0原文

我在使用 simplecursor 适配器时遇到了 listview 的奇怪问题。这是我在cursorAdapter 中实现的runQueryOnBackgroundThread 错误

08-31 21:50:14.540: INFO/dalvikvm(12195): Uncaught exception thrown by finalizer (will be discarded):
08-31 21:50:14.540: INFO/dalvikvm(12195): java.lang.IllegalStateException: Binder has been finalized!
08-31 21:50:14.540: INFO/dalvikvm(12195):     at android.os.BinderProxy.transact(Native Method)
08-31 21:50:14.540: INFO/dalvikvm(12195):     at android.database.BulkCursorProxy.close(BulkCursorNative.java:289)
08-31 21:50:14.540: INFO/dalvikvm(12195):     at android.database.BulkCursorToCursorAdaptor.close(BulkCursorToCursorAdaptor.java:141)
08-31 21:50:14.540: INFO/dalvikvm(12195):     at android.database.CursorWrapper.close(CursorWrapper.java:43)
08-31 21:50:14.540: INFO/dalvikvm(12195):     at android.content.ContentResolver$CursorWrapperInner.close(ContentResolver.java:1575)
08-31 21:50:14.540: INFO/dalvikvm(12195):     at android.content.ContentResolver$CursorWrapperInner.finalize(ContentResolver.java:1586)
08-31 21:50:14.540: INFO/dalvikvm(12195):     at dalvik.system.NativeStart.run(Native Method)

,该错误是由活动中的Filter.filter 方法调用调用的。所以我的活动中没有任何光标的参考。每次我查询时,我都会在游标适配器中获得游标更改回调,该适配器扩展了 SimpleCursorAdapter

下面是它的样子

public void changeCursor(Cursor cursor) {
        Log.d(TAG, "Cursor changed ... **************** "+ cursor);
        if (cursor != null) {
            setLoading(false);
        }
        super.changeCursor(cursor);
        initSectionHeaders(cursor);
        notifyDataSetChanged();
    }

runQueryOnBackgroundThread() 只是从查询返回游标。

我是否遗漏了什么,应该关闭光标还是在任何地方管理光标?对线索的任何帮助都会有很大帮助。提前致谢。

**更新** 我的适配器的片段

public Cursor runQueryOnBackgroundThread(CharSequence constraint) {
        Log.d(TAG, "runQueryOnBackgroundThread "+ constraint);
        if (getFilterQueryProvider() != null) {
            return getFilterQueryProvider().runQuery(constraint);
        }
        return doQuery(constraint, People.CONTENT_URI);
    }

private Cursor doQuery(CharSequence constraint, Uri queryUri){
        StringBuilder selectionStrBuilder = new StringBuilder();
        String selectionArgs[] = null;
        String selection = null;

        if(constraint!=null){
            //ADDING GLOB so that the sql query can interpret linux style wildcards
            selectionStrBuilder.append("(UPPER(").append(People.DISPLAY_NAME).append(") GLOB ?)");

            selection = selectionStrBuilder.toString();
            String constraintStr = constraint.toString().toUpperCase();

            //prepend and append '*' - TODO improve
            constraintStr = "*"+ constraintStr;
            constraintStr +="*";

            selectionArgs = new String[]{constraintStr};
        }
        ContentResolver contentRes = context.getContentResolver();
        return contentRes.query(queryUri, null, selection, selectionArgs, People.DISPLAY_NAME);
    }

除了这两个函数之外,我还重写了之前发布的changeCursor 和使用绑定视图中的光标来获取数据。我不会在其他地方关闭或修改游标属性。

我的提供程序 getQuery 函数看起来像

SQLiteDatabase db = dbHelper.getReadableDatabase();
        Cursor c = qb.query(db, projection, selection, selectionArgs, null, null, orderBy);

        // Tell the cursor what uri to watch,
        // so it knows when its source data changes
        c.setNotificationUri(getContext().getContentResolver(), uri);
        return c;

UPDATE2*

当我在模拟器上运行相同的程序时,我得到了这个堆栈跟踪,这与我在平板电脑上看到的非常不同。

01-03 17:21:44.130: ERROR/IMemory(9328): binder=0x3891a0 transaction failed fd=-2147483647, size=0, err=-2147483646 (Unknown error: 2147483646)
01-03 17:21:44.130: ERROR/IMemory(9328): cannot dup fd=-2147483647, size=0, err=-2147483646 (Bad file number)
01-03 17:21:44.130: ERROR/IMemory(9328): cannot map BpMemoryHeap (binder=0x3891a0), size=0, fd=-1 (Bad file number)
01-03 17:21:44.130: WARN/dalvikvm(9328): JNI WARNING: JNI method called with exception raised
01-03 17:21:44.130: WARN/dalvikvm(9328):              in Ldalvik/system/NativeStart;.run ()V (CallStaticVoidMethodV)
01-03 17:21:44.130: WARN/dalvikvm(9328): Pending exception is:
01-03 17:21:44.130: INFO/dalvikvm(9328): Ljava/lang/RuntimeException;: No memory in memObj
01-03 17:21:44.130: INFO/dalvikvm(9328):     at android.database.CursorWindow.native_init(Native Method)
01-03 17:21:44.130: INFO/dalvikvm(9328):     at android.database.CursorWindow.<init>(CursorWindow.java:518)
01-03 17:21:44.130: INFO/dalvikvm(9328):     at android.database.CursorWindow.<init>(CursorWindow.java:27)
01-03 17:21:44.130: INFO/dalvikvm(9328):     at android.database.CursorWindow$1.createFromParcel(CursorWindow.java:493)
01-03 17:21:44.130: INFO/dalvikvm(9328):     at android.database.CursorWindow$1.createFromParcel(CursorWindow.java:491)
01-03 17:21:44.130: INFO/dalvikvm(9328):     at android.content.ContentProviderNative.onTransact(ContentProviderNative.java:103)
01-03 17:21:44.130: INFO/dalvikvm(9328):     at android.os.Binder.execTransact(Binder.java:320)
01-03 17:21:44.130: INFO/dalvikvm(9328):     at dalvik.system.NativeStart.run(Native Method)
01-03 17:21:44.130: INFO/dalvikvm(9328): "Binder Thread #3" prio=5 tid=9 NATIVE
01-03 17:21:44.130: INFO/dalvikvm(9328):   | group="main" sCount=0 dsCount=0 obj=0x405de828 self=0x8e288
01-03 17:21:44.130: INFO/dalvikvm(9328):   | sysTid=9336 nice=0 sched=0/0 cgrp=[fopen-error:24] handle=959480
01-03 17:21:44.130: INFO/dalvikvm(9328):   at dalvik.system.NativeStart.run(Native Method)
01-03 17:21:44.130: ERROR/dalvikvm(9328): VM aborting

并使应用程序崩溃。

我在此处找到了有关此问题的错误报告

有人遇到过同样的问题吗?任何帮助将不胜感激。 谢谢

I have a weird problem with listview, while using simplecursor adapter. Here is the error

08-31 21:50:14.540: INFO/dalvikvm(12195): Uncaught exception thrown by finalizer (will be discarded):
08-31 21:50:14.540: INFO/dalvikvm(12195): java.lang.IllegalStateException: Binder has been finalized!
08-31 21:50:14.540: INFO/dalvikvm(12195):     at android.os.BinderProxy.transact(Native Method)
08-31 21:50:14.540: INFO/dalvikvm(12195):     at android.database.BulkCursorProxy.close(BulkCursorNative.java:289)
08-31 21:50:14.540: INFO/dalvikvm(12195):     at android.database.BulkCursorToCursorAdaptor.close(BulkCursorToCursorAdaptor.java:141)
08-31 21:50:14.540: INFO/dalvikvm(12195):     at android.database.CursorWrapper.close(CursorWrapper.java:43)
08-31 21:50:14.540: INFO/dalvikvm(12195):     at android.content.ContentResolver$CursorWrapperInner.close(ContentResolver.java:1575)
08-31 21:50:14.540: INFO/dalvikvm(12195):     at android.content.ContentResolver$CursorWrapperInner.finalize(ContentResolver.java:1586)
08-31 21:50:14.540: INFO/dalvikvm(12195):     at dalvik.system.NativeStart.run(Native Method)

I have runQueryOnBackgroundThread implemented in my cursorAdapter, which is invoked by the Filter.filter method call from the activity. So I dont have any reference of the cursor in my activity. Everytime I query I understand I get cursor changed callback in my cursor adapter, which extends SimpleCursorAdapter

Here is how it looks

public void changeCursor(Cursor cursor) {
        Log.d(TAG, "Cursor changed ... **************** "+ cursor);
        if (cursor != null) {
            setLoading(false);
        }
        super.changeCursor(cursor);
        initSectionHeaders(cursor);
        notifyDataSetChanged();
    }

runQueryOnBackgroundThread() just returns a cursor from a query.

Am I missing something, should the cursor be closed or managed anywhere? Any help on the leads would greatly help. Thanks in advance.

**UPDATE** Snippets of my adapter

public Cursor runQueryOnBackgroundThread(CharSequence constraint) {
        Log.d(TAG, "runQueryOnBackgroundThread "+ constraint);
        if (getFilterQueryProvider() != null) {
            return getFilterQueryProvider().runQuery(constraint);
        }
        return doQuery(constraint, People.CONTENT_URI);
    }

private Cursor doQuery(CharSequence constraint, Uri queryUri){
        StringBuilder selectionStrBuilder = new StringBuilder();
        String selectionArgs[] = null;
        String selection = null;

        if(constraint!=null){
            //ADDING GLOB so that the sql query can interpret linux style wildcards
            selectionStrBuilder.append("(UPPER(").append(People.DISPLAY_NAME).append(") GLOB ?)");

            selection = selectionStrBuilder.toString();
            String constraintStr = constraint.toString().toUpperCase();

            //prepend and append '*' - TODO improve
            constraintStr = "*"+ constraintStr;
            constraintStr +="*";

            selectionArgs = new String[]{constraintStr};
        }
        ContentResolver contentRes = context.getContentResolver();
        return contentRes.query(queryUri, null, selection, selectionArgs, People.DISPLAY_NAME);
    }

Other than these two functions I have changeCursor overridden as posted earlier and use the cursor in bind view to get the data. I don't close or modify cursor properties anywhere else.

My provider getQuery function looks like

SQLiteDatabase db = dbHelper.getReadableDatabase();
        Cursor c = qb.query(db, projection, selection, selectionArgs, null, null, orderBy);

        // Tell the cursor what uri to watch,
        // so it knows when its source data changes
        c.setNotificationUri(getContext().getContentResolver(), uri);
        return c;

UPDATE2*

When I run the same program on the emulator I get this stack trace, which is very different from what I see on the tablet.

01-03 17:21:44.130: ERROR/IMemory(9328): binder=0x3891a0 transaction failed fd=-2147483647, size=0, err=-2147483646 (Unknown error: 2147483646)
01-03 17:21:44.130: ERROR/IMemory(9328): cannot dup fd=-2147483647, size=0, err=-2147483646 (Bad file number)
01-03 17:21:44.130: ERROR/IMemory(9328): cannot map BpMemoryHeap (binder=0x3891a0), size=0, fd=-1 (Bad file number)
01-03 17:21:44.130: WARN/dalvikvm(9328): JNI WARNING: JNI method called with exception raised
01-03 17:21:44.130: WARN/dalvikvm(9328):              in Ldalvik/system/NativeStart;.run ()V (CallStaticVoidMethodV)
01-03 17:21:44.130: WARN/dalvikvm(9328): Pending exception is:
01-03 17:21:44.130: INFO/dalvikvm(9328): Ljava/lang/RuntimeException;: No memory in memObj
01-03 17:21:44.130: INFO/dalvikvm(9328):     at android.database.CursorWindow.native_init(Native Method)
01-03 17:21:44.130: INFO/dalvikvm(9328):     at android.database.CursorWindow.<init>(CursorWindow.java:518)
01-03 17:21:44.130: INFO/dalvikvm(9328):     at android.database.CursorWindow.<init>(CursorWindow.java:27)
01-03 17:21:44.130: INFO/dalvikvm(9328):     at android.database.CursorWindow$1.createFromParcel(CursorWindow.java:493)
01-03 17:21:44.130: INFO/dalvikvm(9328):     at android.database.CursorWindow$1.createFromParcel(CursorWindow.java:491)
01-03 17:21:44.130: INFO/dalvikvm(9328):     at android.content.ContentProviderNative.onTransact(ContentProviderNative.java:103)
01-03 17:21:44.130: INFO/dalvikvm(9328):     at android.os.Binder.execTransact(Binder.java:320)
01-03 17:21:44.130: INFO/dalvikvm(9328):     at dalvik.system.NativeStart.run(Native Method)
01-03 17:21:44.130: INFO/dalvikvm(9328): "Binder Thread #3" prio=5 tid=9 NATIVE
01-03 17:21:44.130: INFO/dalvikvm(9328):   | group="main" sCount=0 dsCount=0 obj=0x405de828 self=0x8e288
01-03 17:21:44.130: INFO/dalvikvm(9328):   | sysTid=9336 nice=0 sched=0/0 cgrp=[fopen-error:24] handle=959480
01-03 17:21:44.130: INFO/dalvikvm(9328):   at dalvik.system.NativeStart.run(Native Method)
01-03 17:21:44.130: ERROR/dalvikvm(9328): VM aborting

And Crashes the application.

I found a bug report on this issue here

Anyone faced the same problem? Any help would be appreciated.
Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

内心荒芜 2024-12-09 13:07:47

啊终于找到了解决这个奇怪问题的方法。只是框架的其他部分中的某些光标打开了。关闭时有效。

Ah finally found a soln to this freaking issue. Just that some cursor in some other part of the framework was open. Works when closed.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文