应用程序崩溃按钮单击我怎么能解决它的android工作室

问题描述 投票:-2回答:2

这是代码:

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();
    }
}
android fragment onclicklistener android-contacts buttonclick
2个回答
0
投票

首先,你必须检查是否有你的findviewbyid按钮。如果不是那么首先。

如果你已经完成了这一点,那么在单击按钮后检查Android studio中的Logcat部分。你将完全理由在那里崩溃你的应用程序。


0
投票

您可能遇到导致崩溃的以下任何情况:

  • 您的按钮未在主类的onCreate()方法中正确声明和启动。因此,请检查您的onCreate()方法中是否包含以下代码: Button button = (Button) findViewById(R.id.button);
  • 您的应用从我认为的用户设备中读取联系人。在这种情况下,请检查您是否有必要阅读联系人的权限。为此,只需检查以下行是否包含在AndroidManifest.xml文件中: <uses-permission android:name="android.permission.READ_CONTACTS" />
  • 检查您是否有权调用适当的意图。

您的logcat跟踪将包含导致崩溃的确切代码行和错误代码。所以如果你在这里粘贴logcat信息,你可以得到帮助。

© www.soinside.com 2019 - 2024. All rights reserved.