App crashing on buttonclick how can I solved it an

2019-09-21 09:11发布

问题:

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();
    }
}

回答1:

First of all you have to check that have you findviewbyid of button or not.if not than first do that.

if you have done that than after clicking button check Logcat section in Android studio.you will get perfect reason of crashing your application over there.



回答2:

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.