0

I have tried soo much to code in many different ways but I was not Successful, this what I have done Please tell me where I've wrong and What I should do.

username = findViewById(R.id.usernamem);
Contacts = findViewById(R.id.loadConduct);
Contacts.setOnClickListener(new View.OnClickListener() {
  @Override
  public void onClick(View v) {
    Intent ContactsIntent = getPackageManager().getLaunchIntentForPackage("Contacts");
    try {
      startActivity(ContactsIntent);
    } catch (NullPointerException ignored) {
  }
  ContentResolver resolver = getContentResolver();
  @SuppressLint("Recycle") Cursor cursor = resolver.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
  assert cursor != null;
  while (cursor.moveToNext()) {
    String id = getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
    String Name = getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
STANLEY
  • 19
  • 4
  • 1
    Please specify what you are trying to achieve and what is wrong? – Woworks Jan 18 '20 at 10:25
  • welcome to stack overflow :D while you _have_ included your current code (good start) please _also_ tell us what you're trying to achieve and what currently isn't working as you expect it to. please do this by [editing](https://stackoverflow.com/posts/59799537/edit) your original question to include this information – a_local_nobody Jan 18 '20 at 10:39
  • I am trying to create a button that can load contacts from my phone book so that they can start a chat,,, in short, am creating a Chat app – STANLEY Jan 18 '20 at 10:41
  • please edit your question to explain what isn't working related to your code and what you're trying to achieve – a_local_nobody Jan 18 '20 at 10:42
  • The button is not loading the Contacts – STANLEY Jan 18 '20 at 10:46
  • I've used logcat to check the error and( startActivity(ContactsIntent)) in startactivity is where the problem is,,,, – STANLEY Jan 18 '20 at 11:33
  • Seems like you're calling startActivity() method inside View.OnClickListener interface but startActivty() method is a member of Activity class. Could that be the source of your problem?. Can you try calling it like MyActivity.this.startActivity(ContactsIntent) – emrah Jan 18 '20 at 11:47
  • Mr emrah, I have tried calling it but unfortunately, it still crashes onClick. – STANLEY Jan 18 '20 at 13:55

0 Answers0