如何读取手机中的所有联系人,包括Google联系人,存入数组列表?

huangapple go评论107阅读模式
英文:

How to read all contacts in phone, including google contacts, into an array list?

问题

  1. 我尝试过这个在这里它只读取了4个联系人但没有读取所有联系人
  2. 无法启动活动出现以下问题
  3. ComponentInfo{com.developer.sparty/com.developer.sparty.DashboardActivity}: java.lang.NullPointerException: println 需要一条消息
  4. at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3632)
  5. at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3784)
  6. at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
  7. at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
  8. at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
  9. at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2270)
  10. 我的 `DashboardActivity` 代码如下
  11. public class DashboardActivity extends AppCompatActivity {
  12. ActionBar actionBar;
  13. ArrayList<CONTACTS_DATA> listOfContacts;
  14. public static final int REQUEST_READ_CONTACTS = 79;
  15. @Override
  16. protected void onCreate(Bundle savedInstanceState) {
  17. super.onCreate(savedInstanceState);
  18. setContentView(R.layout.activity_dashboard);
  19. actionBar = getSupportActionBar();
  20. actionBar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#0582CA")));
  21. //on start default
  22. actionBar.setTitle(" " + "Chats");
  23. actionBar.setDisplayShowHomeEnabled(true);
  24. actionBar.setLogo(R.drawable.logo);
  25. actionBar.setDisplayUseLogoEnabled(true);
  26. actionBar.show();
  27. listOfContacts = new ArrayList<>();
  28. if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.READ_CONTACTS)
  29. == PackageManager.PERMISSION_GRANTED) {
  30. ReadContactsAndShowUsers();
  31. } else {
  32. ActivityCompat.requestPermissions(this, new String[]{android.Manifest.permission.READ_CONTACTS},
  33. REQUEST_READ_CONTACTS);
  34. }
  35. }
  36. public class CONTACTS_DATA {
  37. public String contact_data_name;
  38. public String contact_data_phoneNo;
  39. }
  40. @Override
  41. public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
  42. switch (requestCode) {
  43. case REQUEST_READ_CONTACTS: {
  44. if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
  45. ReadContactsAndShowUsers();
  46. } else {
  47. Toast.makeText(this, "需要权限", Toast.LENGTH_SHORT).show();
  48. }
  49. return;
  50. }
  51. }
  52. }
  53. private void ReadContactsAndShowUsers() {
  54. ContentResolver cr = getContentResolver();
  55. Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,
  56. null, null, null, null);
  57. if ((cur!=null?cur.getCount():0) > 0) {
  58. while (cur!=null && cur.moveToNext()) {
  59. CONTACTS_DATA contacts_data=new CONTACTS_DATA();
  60. String id = cur.getString(
  61. cur.getColumnIndex(ContactsContract.Contacts._ID));
  62. String name = cur.getString(cur.getColumnIndex(
  63. ContactsContract.Contacts.DISPLAY_NAME));
  64. contacts_data.contact_data_name=name;
  65. if (cur.getInt(cur.getColumnIndex( ContactsContract.Contacts.HAS_PHONE_NUMBER)) > 0) {
  66. Cursor pCur = cr.query(
  67. ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
  68. null,
  69. ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?",
  70. new String[]{id}, null);
  71. while (pCur.moveToNext()) {
  72. String phoneNo = pCur.getString(pCur.getColumnIndex(
  73. ContactsContract.CommonDataKinds.Phone.NUMBER));
  74. contacts_data.contact_data_phoneNo=phoneNo;
  75. }
  76. pCur.close();
  77. }
  78. listOfContacts.add(contacts_data);
  79. }
  80. }
  81. if (cur!=null) {
  82. cur.close();
  83. }
  84. for (int i = 0; i < listOfContacts.size(); i++) {
  85. Log.d("联系人", listOfContacts.get(i).contact_data_phoneNo);
  86. Log.d("联系人", listOfContacts.get(i).contact_data_name);
  87. }
  88. }
  89. }
英文:

I have tried this where it reads only 4 contacts but does not read all the contacts?

Unable to start activity and getting the below issue:

  1. ComponentInfo{com.developer.sparty/com.developer.sparty.DashboardActivity}: java.lang.NullPointerException: println needs a message
  2. at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3632)
  3. at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3784)
  4. at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
  5. at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
  6. at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
  7. at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2270)

My DashboardActivity code is

  1. public class DashboardActivity extends AppCompatActivity {
  2. ActionBar actionBar;
  3. ArrayList&lt;CONTACTS_DATA&gt; listOfContacts;
  4. public static final int REQUEST_READ_CONTACTS = 79;
  5. @Override
  6. protected void onCreate(Bundle savedInstanceState) {
  7. super.onCreate(savedInstanceState);
  8. setContentView(R.layout.activity_dashboard);
  9. actionBar = getSupportActionBar();
  10. actionBar.setBackgroundDrawable(new ColorDrawable(Color.parseColor(&quot;#0582CA&quot;)));
  11. //on start default
  12. actionBar.setTitle(&quot; &quot; + &quot;Chats&quot;);
  13. actionBar.setDisplayShowHomeEnabled(true);
  14. actionBar.setLogo(R.drawable.logo);
  15. actionBar.setDisplayUseLogoEnabled(true);
  16. actionBar.show();
  17. listOfContacts = new ArrayList&lt;&gt;();
  18. if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.READ_CONTACTS)
  19. == PackageManager.PERMISSION_GRANTED) {
  20. ReadContactsAndShowUsers();
  21. } else {
  22. ActivityCompat.requestPermissions(this, new String[]{android.Manifest.permission.READ_CONTACTS},
  23. REQUEST_READ_CONTACTS);
  24. }
  25. }
  26. public class CONTACTS_DATA {
  27. public String contact_data_name;
  28. public String contact_data_phoneNo;
  29. }
  30. @Override
  31. public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
  32. switch (requestCode) {
  33. case REQUEST_READ_CONTACTS: {
  34. if (grantResults.length &gt; 0 &amp;&amp; grantResults[0] == PackageManager.PERMISSION_GRANTED) {
  35. ReadContactsAndShowUsers();
  36. } else {
  37. Toast.makeText(this, &quot;Permission is required&quot;, Toast.LENGTH_SHORT).show();
  38. }
  39. return;
  40. }
  41. }
  42. }
  43. private void ReadContactsAndShowUsers() {
  44. ContentResolver cr = getContentResolver();
  45. Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,
  46. null, null, null, null);
  47. if ((cur!=null?cur.getCount():0)&gt;0) {
  48. while (cur!=null&amp;&amp;cur.moveToNext()) {
  49. CONTACTS_DATA contacts_data=new CONTACTS_DATA();
  50. String id = cur.getString(
  51. cur.getColumnIndex(ContactsContract.Contacts._ID));
  52. String name = cur.getString(cur.getColumnIndex(
  53. ContactsContract.Contacts.DISPLAY_NAME));
  54. contacts_data.contact_data_name=name;
  55. if (cur.getInt(cur.getColumnIndex( ContactsContract.Contacts.HAS_PHONE_NUMBER)) &gt; 0) {
  56. Cursor pCur = cr.query(
  57. ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
  58. null,
  59. ContactsContract.CommonDataKinds.Phone.CONTACT_ID + &quot; = ?&quot;,
  60. new String[]{id}, null);
  61. while (pCur.moveToNext()) {
  62. String phoneNo = pCur.getString(pCur.getColumnIndex(
  63. ContactsContract.CommonDataKinds.Phone.NUMBER));
  64. contacts_data.contact_data_phoneNo=phoneNo;
  65. }
  66. pCur.close();
  67. }
  68. listOfContacts.add(contacts_data);
  69. }
  70. }
  71. if (cur!=null) {
  72. cur.close();
  73. }
  74. for (int i = 0; i &lt; listOfContacts.size(); i++) {
  75. Log.d(&quot;CONTACT&quot;, listOfContacts.get(i).contact_data_phoneNo);
  76. Log.d(&quot;CONTACT&quot;, listOfContacts.get(i).contact_data_name);
  77. }
  78. }
  79. }

答案1

得分: 0

将这些代码行放在try-catch块中,因为有一些联系人没有与其关联的电话号码,所以可能会出现null值导致崩溃。

  1. for (int i = 0; i < listOfContacts.size(); i++) {
  2. try{
  3. Log.d("CONTACT", listOfContacts.get(i).contact_data_phoneNo);
  4. } catch(Exception e){
  5. e.printStackTrace();
  6. }
  7. try{
  8. Log.d("CONTACT", listOfContacts.get(i).contact_data_name);
  9. } catch(Exception e){
  10. e.printStackTrace();
  11. }
  12. }
英文:

Put these lines in try catch because there are few contacts which do not have phone number associated with them so, you are getting null and crash.

  1. for (int i = 0; i &lt; listOfContacts.size(); i++) {
  2. try{
  3. Log.d(&quot;CONTACT&quot;, listOfContacts.get(i).contact_data_phoneNo);
  4. } catch(Exception e){
  5. e.printStackTrace();
  6. }
  7. try{
  8. Log.d(&quot;CONTACT&quot;, listOfContacts.get(i).contact_data_name);
  9. } catch(Exception e){
  10. e.printStackTrace();
  11. }
  12. }

huangapple
  • 本文由 发表于 2020年8月13日 13:23:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/63388606.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定