1

In my app I give the user the opportunity to choose a number from his contacts. On my phone,(Samsung Galaxy SPlus) it works every time, without even a warning. I sent this app to some of my friends to test it before I upload it and they said that it crashed every time when choosing a name from the contacts list. I do not have access to any other device right now, so do you see anything problematic in my code?

search_contacts.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
                intent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE);
                startActivityForResult(intent, 1);
            }
        });

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (data != null) {
            Uri uri = data.getData();

            if (uri != null) {
                Cursor c = null;
                try {
                    c = getContentResolver()
                            .query(uri,
                                    new String[] {
                                            ContactsContract.CommonDataKinds.Phone.NUMBER,
                                            ContactsContract.CommonDataKinds.Phone.TYPE },
                                    null, null, null);

                    if (c != null && c.moveToFirst()) {
                        String number = c.getString(0);
                        SharedPreferences settings = PreferenceManager
                                .getDefaultSharedPreferences(MainActivity.this);
                        SharedPreferences.Editor editor1 = settings.edit();
                        editor1.putString("phone", number);
                        editor1.commit();
                        System.out.println("Set phone number to: " + number);
                        send_to = number;
                        text.setText(send_to);
                        // int type = c.getInt(1);
                        // showSelectedNumber(type, number);
                    }
                } finally {
                    if (c != null) {
                        c.close();
                    }
                }
            }
        }
    }
4

0 回答 0