如何查询单个Uri,然后获取指向Uri项的Cursor对象?

时间:2012-04-12 16:29:43

标签: android cursor

Uri喜欢这样:

com.android.contacts/contacts/lookup/0r431-E08CD8E00524/436

这是我的方式:

Uri entityUri = Uri.withAppendedPath(contactUri, Contacts.Entity.CONTENT_DIRECTORY);
Cursor cursor = resolver.query(entityUri, ContactQuery.COLUMNS, null, null, null);

它总是坠毁!请帮助我,谢谢!

The crash log is below:
 V/ContactTileView(  275):     
 mLookupUricontent://com.android.contacts/contacts/lookup/0r80-29121422/77
 V/ContactTileView(  275):       
 entityUricontent://com.android.contacts/contacts/lookup/0r80-29121422/77/entities
 D/AndroidRuntime(  275): Shutting down VM
 W/dalvikvm(  275): threadid=1: thread exiting with uncaught exception               
 (group=0x409f61f8)
 I/am_crash(   88):   
[275,com.android.contacts,572997,android.database.CursorIndexOutOfBoundsException,Index -1 requested, with a size of 2,AbstractCursor.java,434]

我直接使用查找键:

Cursor cursor = resolver.query(lookupUri, ContactQuery.COLUMNS, null, null, null);

崩溃日志略有不同:

/ContactTileView(  276):     
mLookupUricontent://com.android.contacts/contacts/lookup/0r285-E00EFAE00580/286
D/AndroidRuntime(  276): Shutting down VM
W/dalvikvm(  276): threadid=1: thread exiting with uncaught exception(group=0x409de1f8)
I/content_query_sample(  276): [content://com.android.contacts/contacts/lookup/0r285-E00EFAE00580/286,_id/display_name/display_name_alt/sort_key/starred/contact_presence/contact_status/photo_id/photo_thumb_uri/lookup/phonetic_name/has_phone_number/is_user_profile,,,41,com.android.contacts,9]
I/am_crash(   88):    
[276,com.android.contacts,572997,android.database.CursorIndexOutOfBoundsException,Index -1 requested, with a size of 1,AbstractCursor.java,434]
D/dalvikvm(  276): GC_CONCURRENT freed 111K, 3% free 7697K/7879K, paused 2ms+3ms
E/StrictMode(  276): A resource was acquired at attached stack trace but never released. See java.io.Closeable for information on avoiding resource leaks.
E/StrictMode(  276): java.lang.Throwable: Explicit termination method 'close' not called
E/StrictMode(  276):    at dalvik.system.CloseGuard.open(CloseGuard.java:184)
E/StrictMode(  276):    at android.content.ContentResolver$CursorWrapperInner.<init>(ContentResolver.java:1581)
E/StrictMode(  276):    at android.content.ContentResolver.query(ContentResolver.java:320)
E/StrictMode(  276):    at com.android.contacts.list.ContactTileView.queryForCursor(ContactTileView.java:249)
E/StrictMode(  276):    at com.android.contacts.list.ContactTileListContextMenuAdapter.onLongClick(ContactTileListContextMenuAdapter.java:83)
E/StrictMode(  276):    at com.android.contacts.list.ContactTileView$2.onLongClick(ContactTileView.java:121)
E/StrictMode(  276):    at android.view.View.performLongClick(View.java:3498)
E/StrictMode(  276):    at android.view.View$CheckForLongPress.run(View.java:13961)
E/StrictMode(  276):    at android.os.Handler.handleCallback(Handler.java:605)
E/StrictMode(  276):    at android.os.Handler.dispatchMessage(Handler.java:92)
E/StrictMode(  276):    at android.os.Looper.loop(Looper.java:137)
E/StrictMode(  276):    at android.app.ActivityThread.main(ActivityThread.java:4340)
E/StrictMode(  276):    at java.lang.reflect.Method.invokeNative(Native Method)
E/StrictMode(  276):    at java.lang.reflect.Method.invoke(Method.java:511)
E/StrictMode(  276):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
E/StrictMode(  276):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
E/StrictMode(  276):    at dalvik.system.NativeStart.main(Native Method)

最重要的是,我认为我的要求是我可以通过查询uri获取光标。然后我可以通过此光标从此联系人获取所有信息。非常感谢。

2 个答案:

答案 0 :(得分:3)

我已经解决了我的问题!这很简单!我的代码没问题,但我必须在调用查询后调用curosr.moveToFist。

答案 1 :(得分:0)

我遇到了同样的问题,并且当我不再需要使用游标时调用close方法解决了这个问题。