Android 光标错误,从电话联系人列表中获取电话联系人
我知道有很多关于如何从联系人列表中选择电话联系人的答案,我在过去的两个小时里一直在查看这些答案。我的片段中有onActivityResult()代码,但在查询uri时,我一直遇到这个游标错误。请看一看Android 光标错误,从电话联系人列表中获取电话联系人,android,cursor,uri,android-contacts,Android,Cursor,Uri,Android Contacts,我知道有很多关于如何从联系人列表中选择电话联系人的答案,我在过去的两个小时里一直在查看这些答案。我的片段中有onActivityResult()代码,但在查询uri时,我一直遇到这个游标错误。请看一看 package zafus.addressbook; import android.app.Activity; import android.content.Intent; import android.database.Cursor; import android.net.Uri; impor
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);
}*/
}
包zafus.addressbook;
导入android.app.Activity;
导入android.content.Intent;
导入android.database.Cursor;
导入android.net.Uri;
导入android.os.Bundle;
导入android.provider.contacts合同;
导入android.support.v4.app.Fragment;
导入android.view.LayoutInflater;
导入android.view.view;
导入android.view.ViewGroup;
导入android.widget.Button;
导入android.widget.EditText;
导入android.widget.LinearLayout;
导入android.widget.RelativeLayout;
导入android.widget.TextView;
导入android.widget.Toast;
/**
*一个简单的{@link Fragment}子类。
*包含此片段的活动必须实现
*{@link//ContactAddressEntryFragment//OnFragmentInteractionListener}接口
*处理交互事件。
*使用{@link ContactAddressEntryFragment#newInstance}工厂方法
*创建此片段的实例。
*/
公共类ContactAddressEntryFragment扩展了片段{
//TODO:重命名参数参数,选择匹配的名称
//片段初始化参数,例如ARG_ITEM_NUMBER
私有静态最终字符串ARG_PARAM1=“PARAM1”;
私有静态最终字符串ARG_PARAM2=“PARAM2”;
//TODO:重命名和更改参数类型
私有字符串mParam1;
私有字符串mParam2;
私有OnFragmentInteractionListener mListener;
私人最终int PICK_CONTACT=1;
/**
*使用此工厂方法创建的新实例
*使用提供的参数创建此片段。
*
*@param param1参数1。
*@param param2参数2。
*@返回片段ContactAddressEntryFragment的新实例。
*/
//TODO:重命名和更改参数的类型和数量
公共静态ContactAddressEntryFragment newInstance(字符串param1,字符串param2){
ContactAddressEntryFragment=新的ContactAddressEntryFragment();
Bundle args=新Bundle();
args.putString(ARG_PARAM1,PARAM1);
args.putString(ARG_PARAM2,PARAM2);
fragment.setArguments(args);
返回片段;
}
public ContactAddressEntryFragment(){
//必需的空公共构造函数
}
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
如果(getArguments()!=null){
mParam1=getArguments().getString(ARG_PARAM1);
mParam2=getArguments().getString(ARG_PARAM2);
}
}
私有视图行;
@凌驾
创建视图上的公共视图(布局、充气机、视图组容器、,
Bundle savedInstanceState){
//为该碎片膨胀布局
row=充气机。充气(R.layout.fragment\u contact\u address\u entry,container,false);
TextView ch1=(TextView)row.findViewById(R.id.notincontact复选框);
ch1.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
LinearLayout l=(LinearLayout)行。findViewById(R.id.从联系人中选择按钮\u LinearLayout);
l、 设置可见性(View.GONE);
RelativeLayout r=(RelativeLayout)row.findViewById(r.id.random);
r、 设置可见性(View.VISIBLE);
l=(LinearLayout)行。findViewById(R.id.从联系人中选择按钮LinearLayout);
l、 设置可见性(View.VISIBLE);
}
});
ch1=(TextView)row.findViewById(R.id.incontact复选框);
ch1.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
LinearLayout l=(LinearLayout)行。findViewById(R.id.从联系人中选择按钮\u LinearLayout);
l、 设置可见性(View.VISIBLE);
RelativeLayout r=(RelativeLayout)row.findViewById(r.id.random);
r、 设置可见性(View.GONE);
l=(LinearLayout)行。findViewById(R.id.从联系人中选择按钮LinearLayout);
l、 设置可见性(View.GONE);
}
});
按钮b=(按钮)行。findViewById(R.id.select\u from\u contacts\u按钮);
b、 setOnClickListener(新视图。OnClickListener(){
@凌驾
公共void onClick(视图v){
意向意向=新意向(Intent.ACTION\u PICK,Contacts contract.Contacts.CONTENT\u URI);
startActivityForResult(意向、选择联系人);
}
});
返回行;
}
@凌驾
ActivityResult上的公共无效(int-reqCode、int-resultCode、意图数据){
super.onActivityResult(请求代码、结果代码、数据);
Toast.makeText(getActivity(),“OnResult片段调用”,Toast.LENGTH_LONG.show();
Uri contactData=data.getData();
开关(reqCode){
案例选取联系人:
if(resultCode==Activity.RESULT\u确定){
Cursor Cursor=getActivity().getContentResolver().query(contactData,null,null,null);
试一试{
int contactIdIdx=cursor.getColumnIndex(ContactsContract.CommonDataTypes.Phone.\u ID);
int nameIdx=cursor.getColumnIndex(ContactsContract.CommonDataTypes.Phone.DISPLAY\u NAME);
int phoneNumberIdx=cursor.getColumnIndex(ContactsContract.CommonDataTypes.Phone.NUMBER);
int photoidix=cursor.getColumnIndex(ContactsContract.CommonDataTypes.Phone.PHOTO_ID);
int emailIdx=cursor.getColumnIndex(ContactsContract.CommonDataTypes.Email.ADDRESS);
铜
Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(intent, PICK_CONTACT);
@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=ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
if(data!=null) {
contactData = data.getData();
}
switch (reqCode) {
case PICK_CONTACT:
if (resultCode == Activity.RESULT_OK) {
Cursor c = getActivity().getContentResolver().query(contactData, null, null, null, null);
String name="",email="",number="";
if (c.moveToFirst()) {
String id =c.getString(c.getColumnIndexOrThrow(ContactsContract.Contacts._ID));
String hasPhone =c.getString(c.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
//String hasEmail =c.getString(c.getColumnIndex(ContactsContract.Contacts.Data.DATA1.));
if (hasPhone.equalsIgnoreCase("1")) {
Cursor phones = getActivity().getContentResolver().query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = " + id,
null, null);
phones.moveToFirst();
number = phones.getString(phones.getColumnIndex("data1"));
phones.close();
}
Cursor emailCur = getActivity().getContentResolver().query(
ContactsContract.CommonDataKinds.Email.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?",
new String[]{id}, null);
while (emailCur.moveToNext()) {
// This would allow you get several email addresses
// if the email addresses were stored in an array
if(emailCur.getString(
emailCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA))!="")
email = emailCur.getString(
emailCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
String emailType = emailCur.getString(
emailCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.TYPE));
}
emailCur.close();
name = c.getString(c.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
c.close();
}
setData(name,number,email);
// Do something with the phone number...
}
}
}
@Override
public void onActivityResult(int reqCode, int resultCode, Intent data) {
super.onActivityResult(reqCode, resultCode, data);
}