光标错误,从手机联系人列表中获取手机联系人

时间:2015-09-18 19:50:35

标签: android cursor uri android-contacts

我知道有很多关于如何从联系人列表中选择电话联系人的答案,并且我已经过去了2个小时。我的片段中有onActivityResult()代码但是我在查询uri时不断收到此游标错误。请看看

package zafus.addressbook;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;


/**
 * A simple {@link Fragment} subclass.
 * Activities that contain this fragment must implement the
 * {@link //ContactAddressEntryFragment.//OnFragmentInteractionListener} interface
 * to handle interaction events.
 * Use the {@link ContactAddressEntryFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class ContactAddressEntryFragment extends Fragment {
    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;

    private OnFragmentInteractionListener mListener;
    private final int PICK_CONTACT=1;

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment ContactAddressEntryFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static ContactAddressEntryFragment newInstance(String param1, String param2) {
        ContactAddressEntryFragment fragment = new ContactAddressEntryFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    public ContactAddressEntryFragment() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }
    private View row;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        row=inflater.inflate(R.layout.fragment_contact_address_entry, container, false);

        TextView ch1=(TextView)row.findViewById(R.id.NotInContactCheckBox);
        ch1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                    LinearLayout l=(LinearLayout)row.findViewById(R.id.select_from_contacts_button_LinearLayout);
                    l.setVisibility(View.GONE);
                    RelativeLayout r=(RelativeLayout)row.findViewById(R.id.random);
                    r.setVisibility(View.VISIBLE);
                    l=(LinearLayout)row.findViewById(R.id.select_from_IN_contacts_button_LinearLayout);
                    l.setVisibility(View.VISIBLE);
                }

        });


        ch1=(TextView)row.findViewById(R.id.InContactCheckBox);

        ch1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                    LinearLayout l=(LinearLayout)row.findViewById(R.id.select_from_contacts_button_LinearLayout);
                    l.setVisibility(View.VISIBLE);
                    RelativeLayout r=(RelativeLayout)row.findViewById(R.id.random);
                    r.setVisibility(View.GONE);
                    l=(LinearLayout)row.findViewById(R.id.select_from_IN_contacts_button_LinearLayout);
                    l.setVisibility(View.GONE);

            }
        });

        Button b=(Button)row.findViewById(R.id.select_from_contacts_button);
        b.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
                startActivityForResult(intent, PICK_CONTACT);
            }
        });


        return row;
    }



    @Override
    public void onActivityResult(int reqCode, int resultCode, Intent data) {
        super.onActivityResult(reqCode, resultCode, data);

        Toast.makeText(getActivity(),"OnResult fragment called",Toast.LENGTH_LONG).show();
        Uri contactData = data.getData();
        switch (reqCode) {
            case PICK_CONTACT:
                if (resultCode == Activity.RESULT_OK) {
                    Cursor cursor = getActivity().getContentResolver().query(contactData, null, null, null, null);
                    try {
                        int contactIdIdx = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone._ID);
                        int nameIdx = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
                        int phoneNumberIdx = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
                        int photoIdIdx = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.PHOTO_ID);
                        int emailIdx = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Email.ADDRESS);
                        cursor.moveToFirst();
                        String name="";String phoneNumber="";String emailAddress="";

                        //phoneNumber =       cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.NUMBER));
                        //name =              cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
                        //emailAddress =              cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Email.ADDRESS));
                        do {
                            String idContact = cursor.getString(contactIdIdx);
                            name = cursor.getString(nameIdx);
                            phoneNumber = cursor.getString(phoneNumberIdx);
                            emailAddress=cursor.getString(emailIdx);
                        } while (cursor.moveToNext());
                        setData(name,phoneNumber,emailAddress);
                    } catch (Exception e) {
                        e.printStackTrace();
                        Toast.makeText(getActivity(),e.getMessage(),Toast.LENGTH_LONG).show();
                    } finally {
                        if (cursor != null) {
                            cursor.close();
                        }
                    }
                }
                break;
        }
    }


public void setData(String n,String p,String e){
   // CheckBox ch=(CheckBox)row.findViewById(R.id.InContactCheckBox);
    LinearLayout l=(LinearLayout)row.findViewById(R.id.select_from_contacts_button_LinearLayout);
    //ch.setChecked(false);
    l.setVisibility(View.GONE);
    RelativeLayout r=(RelativeLayout)row.findViewById(R.id.random);
    r.setVisibility(View.VISIBLE);
    l=(LinearLayout)row.findViewById(R.id.select_from_IN_contacts_button_LinearLayout);
    l.setVisibility(View.VISIBLE);


    EditText editText = (EditText)row.findViewById(R.id.contact_address_name_entry);
    editText.setText(n, TextView.BufferType.EDITABLE);

    editText = (EditText)row.findViewById(R.id.contact_address_number_entry);
    editText.setText(p, TextView.BufferType.EDITABLE);

    editText = (EditText)row.findViewById(R.id.contact_address_email_entry);
    editText.setText(e, TextView.BufferType.EDITABLE);


}

    // TODO: Rename method, update argument and hook method into UI event
    public void onButtonPressed(Uri uri) {
        if (mListener != null) {
           // mListener.onFragmentInteraction(uri);
        }
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        try {
            mListener = (OnFragmentInteractionListener) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString()
                    + " must implement OnFragmentInteractionListener");
        }
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mListener = null;
    }

    /**
     * This interface must be implemented by activities that contain this
     * fragment to allow an interaction in this fragment to be communicated
     * to the activity and potentially other fragments contained in that
     * activity.
     * <p/>
     * See the Android Training lesson <a href=
     * "http://developer.android.com/training/basics/fragments/communicating.html"
     * >Communicating with Other Fragments</a> for more information.
     */
    /*public interface OnFragmentInteractionListener {
        // TODO: Update argument type and name
        public void onFragmentInteraction(Uri uri);
    }*/

}

这是错误说的: enter image description here

0 个答案:

没有答案
相关问题