1

我现在是 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
4

1 回答 1

1

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

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

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

于 2013-03-23T17:19:34.113 回答