如何将手机中的所有联系人(包括谷歌联系人)读入数组列表?
Posted
技术标签:
【中文标题】如何将手机中的所有联系人(包括谷歌联系人)读入数组列表?【英文标题】:How to read all contacts in phone, including google contacts, into an array list? 【发布时间】:2020-12-02 21:58:18 【问题描述】:我试过这个,它只读取 4 个联系人,但不读取所有联系人?
无法开始活动并出现以下问题:
ComponentInfocom.developer.sparty/com.developer.sparty.DashboardActivity: java.lang.NullPointerException: println needs a message
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3632)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3784)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2270)
我的DashboardActivity
代码是
public class DashboardActivity extends AppCompatActivity
ActionBar actionBar;
ArrayList<CONTACTS_DATA> listOfContacts;
public static final int REQUEST_READ_CONTACTS = 79;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dashboard);
actionBar = getSupportActionBar();
actionBar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#0582CA")));
//on start default
actionBar.setTitle(" " + "Chats");
actionBar.setDisplayShowHomeEnabled(true);
actionBar.setLogo(R.drawable.logo);
actionBar.setDisplayUseLogoEnabled(true);
actionBar.show();
listOfContacts = new ArrayList<>();
if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.READ_CONTACTS)
== PackageManager.PERMISSION_GRANTED)
ReadContactsAndShowUsers();
else
ActivityCompat.requestPermissions(this, new String[]android.Manifest.permission.READ_CONTACTS,
REQUEST_READ_CONTACTS);
public class CONTACTS_DATA
public String contact_data_name;
public String contact_data_phoneNo;
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults)
switch (requestCode)
case REQUEST_READ_CONTACTS:
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED)
ReadContactsAndShowUsers();
else
Toast.makeText(this, "Permission is required", Toast.LENGTH_SHORT).show();
return;
private void ReadContactsAndShowUsers()
ContentResolver cr = getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,
null, null, null, null);
if ((cur!=null?cur.getCount():0)>0)
while (cur!=null&&cur.moveToNext())
CONTACTS_DATA contacts_data=new CONTACTS_DATA();
String id = cur.getString(
cur.getColumnIndex(ContactsContract.Contacts._ID));
String name = cur.getString(cur.getColumnIndex(
ContactsContract.Contacts.DISPLAY_NAME));
contacts_data.contact_data_name=name;
if (cur.getInt(cur.getColumnIndex( ContactsContract.Contacts.HAS_PHONE_NUMBER)) > 0)
Cursor pCur = cr.query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?",
new String[]id, null);
while (pCur.moveToNext())
String phoneNo = pCur.getString(pCur.getColumnIndex(
ContactsContract.CommonDataKinds.Phone.NUMBER));
contacts_data.contact_data_phoneNo=phoneNo;
pCur.close();
listOfContacts.add(contacts_data);
if (cur!=null)
cur.close();
for (int i = 0; i < listOfContacts.size(); i++)
Log.d("CONTACT", listOfContacts.get(i).contact_data_phoneNo);
Log.d("CONTACT", listOfContacts.get(i).contact_data_name);
【问题讨论】:
【参考方案1】:将这些行放在 try catch 中,因为很少有联系人没有与之关联的电话号码,所以你会得到 null 并崩溃。
for (int i = 0; i < listOfContacts.size(); i++)
try
Log.d("CONTACT", listOfContacts.get(i).contact_data_phoneNo);
catch(Exception e)
e.printStackTrace();
try
Log.d("CONTACT", listOfContacts.get(i).contact_data_name);
catch(Exception e)
e.printStackTrace();
【讨论】:
以上是关于如何将手机中的所有联系人(包括谷歌联系人)读入数组列表?的主要内容,如果未能解决你的问题,请参考以下文章