从联系人选择器意图中读取联系信息时出错

时间:2017-06-08 17:38:23

标签: android android-intent

我正在创建一个应用程序,它从联系人意图中读取联系人姓名和号码,并添加与相应联系人相关的注释。我应该从contactscontract读取信息并将其分别设置到编辑文本上。但是它显示了一些错误。 这是我的Add_note.java活动:

package com.example.shubhangi.contactnotes;

import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.provider.ContactsContract;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import java.util.ArrayList;

public class Add_note extends AppCompatActivity {
    private static final int RESULT_PICK_CONTACT=1001;
    private EditText name;
    private EditText number;
    private EditText contactnot;
    ArrayList<NoteView> notes= new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_note);
        name=(EditText) findViewById(R.id.con_name);
        name.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent contactPickerIntent = new Intent(Intent.ACTION_PICK,
                        ContactsContract.CommonDataKinds.Phone.CONTENT_URI);
                startActivityForResult(contactPickerIntent, RESULT_PICK_CONTACT);
            }
        });
        number=(EditText) findViewById(R.id.con_numb);
        contactnot=(EditText) findViewById(R.id.con_note);
        if(contactnot.getText()!=null){
            Button add=(Button)findViewById(R.id.save);
            add.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    String n1=name.getText().toString();
                    String n2=number.getText().toString();
                    String n3=contactnot.getText().toString();
                    NoteView nv =new NoteView();
                    nv.setName_contact(n1);
                    nv.setPhno_contact(n2);
                    nv.setNote_contact(n3);
                    notes.add(nv);
                    Intent i=new Intent(Add_note.this,MainActivity.class);
                    i.putExtra("Note list",notes);
                    startActivity(i);
                }
            });
        }
    }

    @Override
    public void onBackPressed() {
        Intent i=new Intent(Add_note.this,MainActivity.class);
        i.putExtra("Note list",notes);
        startActivity(i);
        super.onBackPressed();
    }

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // check whether the result is ok
        if (resultCode == RESULT_OK) {
            // Check for the request code, we might be usign multiple startActivityForReslut
            switch (requestCode) {
                case RESULT_PICK_CONTACT:
                    contactPicked(data);
                    break;
            }
        } else {
            Toast.makeText(Add_note.this, "Could not load contact", Toast.LENGTH_SHORT).show();
        }
    }
    private void contactPicked(Intent data)
    {
        Cursor cursor;
        try {
            Uri uri = data.getData();
            //Query the content uri
            String id = uri.getLastPathSegment();
            Integer id_c=Integer.parseInt(id);
            cursor = getContentResolver().query(uri, null, null, null, null);

            if(cursor.moveToNext()) {
                int columnIndex_name = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
                String stringName = cursor.getString(columnIndex_name);

                int columnIndex_HASPHONENUMBER = cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER);
                String stringHasPhoneNumber = cursor.getString(columnIndex_HASPHONENUMBER);
                String stringNumber;
                if (stringHasPhoneNumber.equalsIgnoreCase("1")) {
                    Cursor cursorNum = getContentResolver().query(
                            ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                            null,
                            ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "=" + id_c,
                            null,
                            null);
                    int columnIndex_number = cursorNum.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
                    stringNumber = cursorNum.getString(columnIndex_number);
                    number.setText(stringNumber);
                    cursorNum.close();
                }
                name.setText(stringName);
                cursor.close();
            }
        }
        catch (Exception ex)
        {
            ex.printStackTrace();
        }

    }
}

这是我的日志:

android.database.CursorIndexOutOfBoundsException: Index -1 reques android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 0
06-08 22:56:14.349 12446-12446/com.example.shubhangi.contactnotes W/System.err:     at android.database.AbstractCursor.checkPosition(AbstractCursor.java:426)
06-08 22:56:14.349 12446-12446/com.example.shubhangi.contactnotes W/System.err:     at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:146)
06-08 22:56:14.349 12446-12446/com.example.shubhangi.contactnotes W/System.err:     at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:60)
06-08 22:56:14.349 12446-12446/com.example.shubhangi.contactnotes W/System.err:     at android.database.CursorWrapper.getString(CursorWrapper.java:114)
06-08 22:56:14.349 12446-12446/com.example.shubhangi.contactnotes W/System.err:     at com.example.shubhangi.contactnotes.Add_note.contactPicked(Add_note.java:105)
06-08 22:56:14.349 12446-12446/com.example.shubhangi.contactnotes W/System.err:     at com.example.shubhangi.contactnotes.Add_note.onActivityResult(Add_note.java:73)
06-08 22:56:14.349 12446-12446/com.example.shubhangi.contactnotes W/System.err:     at android.app.Activity.dispatchActivityResult(Activity.java:6168)
06-08 22:56:14.350 12446-12446/com.example.shubhangi.contactnotes W/System.err:     at android.app.ActivityThread.deliverResults(ActivityThread.java:3733)
06-08 22:56:14.350 12446-12446/com.example.shubhangi.contactnotes D/ActivityThread: SEND_RESULT handled : 0 / ResultData{token=android.os.BinderProxy@60b629b results[ResultInfo{who=null, request=1001, result=-1, data=Intent { dat=content://com.android.contacts/data/13049 flg=0x1 }}]}
06-08 22:56:14.350 12446-12446/com.example.shubhangi.contactnotes W/System.err:     at android.app.ActivityThread.handleSendResult(ActivityThread.java:3780)
06-08 22:56:14.350 12446-12446/com.example.shubhangi.contactnotes W/System.err:     at android.app.ActivityThread.access$1300(ActivityThread.java:162)
06-08 22:56:14.350 12446-12446/com.example.shubhangi.contactnotes V/ActivityThread: Performing resume of ActivityRecord{2e7cd6aa token=android.os.BinderProxy@60b629b {com.example.shubhangi.contactnotes/com.example.shubhangi.contactnotes.Add_note}} finished=false
06-08 22:56:14.350 12446-12446/com.example.shubhangi.contactnotes W/System.err:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1461)
06-08 22:56:14.350 12446-12446/com.example.shubhangi.contactnotes W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:106)
06-08 22:56:14.350 12446-12446/com.example.shubhangi.contactnotes W/System.err:     at android.os.Looper.loop(Looper.java:189)
06-08 22:56:14.350 12446-12446/com.example.shubhangi.contactnotes W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:5530)
06-08 22:56:14.350 12446-12446/com.example.shubhangi.contactnotes W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
06-08 22:56:14.350 12446-12446/com.example.shubhangi.contactnotes W/System.err:     at java.lang.reflect.Method.invoke(Method.java:372)
06-08 22:56:14.350 12446-12446/com.example.shubhangi.contactnotes W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:950)
06-08 22:56:14.350 12446-12446/com.example.shubhangi.contactnotes W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:745)

1 个答案:

答案 0 :(得分:0)

执行查询后,您必须调用 cursor.moveToFirst() ...

试试这个

    Cursor cursor = getContentResolver().query(Contacts.CONTENT_URI, 
   null, null, null, null);

if(cursor != null && cursor.moveToFirst())
{
   String id = cursor.getString(cur.getColumnIndex(Contacts._ID));

    if (cursor.getCount() > 0) {
        while (cursor.moveToNext()) {
     ...