gpt4 book ai didi

android - 从联系人内容提供商 "Failure delivering result"获取联系人

转载 作者:行者123 更新时间:2023-11-30 03:41:53 30 4
gpt4 key购买 nike

我是 android 的新手,现在正在学习内容提供程序。

试着掌握它的基本窍门,但遇到一个不太确定如何解决的错误。

当一个按钮被点击时这个函数被调用

public void getContact(View v){
Intent i=new Intent(Intent.ACTION_GET_CONTENT);
i.setType((ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE));
startActivityForResult(i, this.contactActId);
}

和 onActivityResult 函数

public void onActivityResult(int requestCode,int resultCode,Intent data){

if(requestCode==this.contactActId){
if(data!=null){
Uri uri=data.getData();
if(uri!=null){
Cursor c=null;
c=getContentResolver().query(uri, new String[]{
ContactsContract.CommonDataKinds.Phone.NUMBER,
ContactsContract.CommonDataKinds.Phone.TYPE
}, null, null, null);

if(c!=null){
String number=c.getString(0);//EXCEPTION COMES HERE
int type=c.getInt(1);
Toast t=Toast.makeText(this, "The number is"+number+"of type"+type,Toast.LENGTH_LONG);
t.setGravity(Gravity.CENTER_HORIZONTAL, 0, 0);
t.show();
}
}

}
}
}

日志

03-23 21:45:04.460: E/AndroidRuntime(1362): FATAL EXCEPTION: main
03-23 21:45:04.460: E/AndroidRuntime(1362): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=4, result=-1, data=Intent { dat=content://com.android.contacts/data/1744 flg=0x1 }} to activity {com.example.contentproviderstest/com.example.contentproviderstest.MainActivity}: android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 1
03-23 21:45:04.460: E/AndroidRuntime(1362): at android.app.ActivityThread.deliverResults(ActivityThread.java:2985)
03-23 21:45:04.460: E/AndroidRuntime(1362): at android.app.ActivityThread.handleSendResult(ActivityThread.java:3028)
03-23 21:45:04.460: E/AndroidRuntime(1362): at android.app.ActivityThread.access$1100(ActivityThread.java:128)
03-23 21:45:04.460: E/AndroidRuntime(1362): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1182)
03-23 21:45:04.460: E/AndroidRuntime(1362): at android.os.Handler.dispatchMessage(Handler.java:99)
03-23 21:45:04.460: E/AndroidRuntime(1362): at android.os.Looper.loop(Looper.java:137)
03-23 21:45:04.460: E/AndroidRuntime(1362): at android.app.ActivityThread.main(ActivityThread.java:4453)
03-23 21:45:04.460: E/AndroidRuntime(1362): at java.lang.reflect.Method.invokeNative(Native Method)
03-23 21:45:04.460: E/AndroidRuntime(1362): at java.lang.reflect.Method.invoke(Method.java:511)
03-23 21:45:04.460: E/AndroidRuntime(1362): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:787)
03-23 21:45:04.460: E/AndroidRuntime(1362): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:554)
03-23 21:45:04.460: E/AndroidRuntime(1362): at dalvik.system.NativeStart.main(Native Method)
03-23 21:45:04.460: E/AndroidRuntime(1362): Caused by: android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 1
03-23 21:45:04.460: E/AndroidRuntime(1362): at android.database.AbstractCursor.checkPosition(AbstractCursor.java:400)
03-23 21:45:04.460: E/AndroidRuntime(1362): at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:136)
03-23 21:45:04.460: E/AndroidRuntime(1362): at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:50)
03-23 21:45:04.460: E/AndroidRuntime(1362): at android.database.CursorWrapper.getString(CursorWrapper.java:114)
03-23 21:45:04.460: E/AndroidRuntime(1362): at com.example.contentproviderstest.MainActivity.onActivityResult(MainActivity.java:62)
03-23 21:45:04.460: E/AndroidRuntime(1362): at android.app.Activity.dispatchActivityResult(Activity.java:4651)
03-23 21:45:04.460: E/AndroidRuntime(1362): at android.app.ActivityThread.deliverResults(ActivityThread.java:2981)
03-23 21:45:04.460: E/AndroidRuntime(1362): ... 11 more

最佳答案

您必须将光标移动到第一个位置,才能获得第一个数据集。说些类似的话

if(c!=null && c.moveToFirst()){
..
}

还要确保你有 <uses-permission android:name="android.permission.READ_CONTACTS" />在您的 list 中。

关于android - 从联系人内容提供商 "Failure delivering result"获取联系人,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15589363/

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