我现在是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();
}
}
}
}
}
logcat的
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
答案 0 :(得分:1)
您必须将光标移动到第一个位置才能获得第一个数据集。说出类似
的内容if(c!=null && c.moveToFirst()){
..
}
还要确保您的清单中有<uses-permission android:name="android.permission.READ_CONTACTS" />
。