-2

Here is the code:

button.setOnClickListener(new View.OnClickListener() {
    @Override public void onClick (View view){
    Intent contactPickerIntent = new Intent(Intent.ACTION_PICK, ContactsContract.CommonDataKinds.Phone.CONTENT_URI);
    startActivityForResult(contactPickerIntent, RESULT_PICK_CONTACT);
}
}); return view;}

public void but(View v) {
    Intent contactPickerIntent = new Intent(Intent.ACTION_PICK, ContactsContract.CommonDataKinds.Phone.CONTENT_URI);
    startActivityForResult(contactPickerIntent, RESULT_PICK_CONTACT);
}

public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == RESULT_OK) {
        switch (requestCode) {
            case RESULT_PICK_CONTACT:
                contactPicked(data);
                break;
        }
    } else {
        Log.e("MainActivity", "Failed to pick contact");
    }
}

private void contactPicked(Intent data) {
    Cursor cursor = null;
    try {
        String phoneNo = null;
        String name = null;
        Uri uri = data.getData();
        cursor = getActivity().getContentResolver().query(uri, null, null, null, null);
        cursor.moveToFirst();
        int phoneIndex = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
        int nameIndex = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
        phoneNo = cursor.getString(phoneIndex);
        name = cursor.getString(nameIndex);
        if (phoneNo.startsWith("+")) {
            if (phoneNo.length() == 13) {
                String str_getMOBILE = phoneNo.substring(4);
                editText.setText(("0") + str_getMOBILE);
            }
            if (phoneNo.length() == 16) {
                String str_getMOBILE = phoneNo.substring(4);
                editText.setText(("0") + str_getMOBILE);
            }
        } else {
            editText.setText(phoneNo);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Shobhit Puri
  • 25,769
  • 11
  • 95
  • 124
  • Masresha it is likely any of the references in your code might be null, or simply your cursor is empty or you don't have enough permissions to use the service you're intending to use. But the point is, is hard to tell from the code yet easy if you look at the Logcat output. Find the tab at the bottom in Android Studio and look for your fatal error there. Good luck! – Eddie Lopez Aug 30 '18 at 21:40
  • Hi Masresha, could you please post your whole class & Log if possible? – khalid3e Aug 30 '18 at 22:03
  • 1
    you need to check the stack trace of your crash, it'll guide you to the exact line that is crashing, see: https://stackoverflow.com/questions/3988788/what-is-a-stack-trace-and-how-can-i-use-it-to-debug-my-application-errors – marmor Sep 02 '18 at 12:58

1 Answers1

0

You may be facing any of the following scenarios that led to the crash :

  • Your button is not declared and initiated properly in the onCreate() method of your main class. So please check if you have the below code in your onCreate() method :

    Button button = (Button) findViewById(R.id.button);

  • Your app reads contacts from the user device I believe. In that case, please check if you have necessary permission to read contacts. For that, just check if the below line is included in the AndroidManifest.xml file :

    <uses-permission android:name="android.permission.READ_CONTACTS" />

  • Check if you have permission to call the appropriate intent.

Your logcat trace will contain the exact line of code that led to the crash and the error code. So if you paste the logcat info here, you can get help.