Search code examples
javaandroidcursorandroid-contactsruntimeexception

RuntimeException on Retrieving Contact Data Android


As a part of a simple practice program I am working on, I would like to retrieve data from my cell phone's contacts, specifically the phone number of a selected contact, or all of them, if there is more than one.

I begin by launching the Contacts application and allowing the selection of a contact. I have a block that has, tried and true, worked to selected the default (or if there is one one, the one) number of a contact, but I would like to draw up a list of all numbers should the contact have many. I am quite out of my element, but once this snippet is up and running I should be back in it.

I first launch the contact application for the selection of a contact:

void getNewTxtAlarmFromContacts(){
    Intent intent = new Intent(Intent.ACTION_PICK,
            ContactsContract.CommonDataKinds.Phone.CONTENT_URI);
    startActivityForResult(intent, REQUEST_CONTACT_PICKER);
}



and then seek to process the results with the snippet that follows: (Modified Source: All phone numbers of one contact for startActivityForResult):


 String id = contactData.getLastPathSegment();
                    Cursor phoneCur = getContentResolver()
                            .query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                                    null,
                                    ContactsContract.CommonDataKinds.Phone.CONTACT_ID
                                            + " = ?", new String[] { id },
                                    null);

                    final ArrayList<String> phonesList = new ArrayList<String>();
                    while (phoneCur.moveToNext()) {
                        String phone = phoneCur
                                .getString(phoneCur
                                        .getColumnIndex(ContactsContract.CommonDataKinds.Phone.DATA));
                        phonesList.add(phone);
                    }
                    phoneCur.close();


Now, I have set this block up to first, as in the source, check that the data received is not null. The next lines, through and up to the closure of the Cursor, were the real purpose of my use of the snippet above. They are supposed to retrieve all numbers for a particular contact, that is, the one selected earlier. They are followed by if-else constructions simply to allow me to handle the numbers as I please. My difficulty surrounds those lines with the cursor. I believe the problem may lie with the arguments of the

.query()

call, since my functional code, the code that chooses only the default number, uses only the first argument. I have persistently received the following error.

02-16 11:53:01.988    3192-3192/com.naja.meno.william.notificationalarm E/ActivityThread﹕ Performing stop of activity that is not resumed: {com.naja.meno.william.notificationalarm/com.naja.meno.william.notificationalarm.StartMenu}
java.lang.RuntimeException: Performing stop of activity that is not resumed: {com.naja.meno.william.notificationalarm/com.naja.meno.william.notificationalarm.StartMenu}
        at android.app.ActivityThread.performStopActivityInner(ActivityThread.java:3362)
        at android.app.ActivityThread.handleStopActivity(ActivityThread.java:3449)
        at android.app.ActivityThread.access$1200(ActivityThread.java:169)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1307)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:146)
        at android.app.ActivityThread.main(ActivityThread.java:5487)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
        at dalvik.system.NativeStart.main(Native Method)


Following this error being caught by the broad catch handler, the system resumes and the first if is executed, suggesting thus that there is no data to be found, even though I only select contacts with many phone numbers.

I am not confident that I know why this error is occurring. The end result should be that I have an ArrayList with all relevant numbers.

Though perhaps it is something else, given my log does not provide a reference to the site of the error. I don't understand what activity I am stopping, according to the error, and what I can do. But that is why I am here. I hope someone can help. Thank you.


Solution

  • I chose to offer this solution to myself... I followed code for selection of the name of the chosen contact, and then iterated through all contacts displaying and logging in my ArrayList all values with a matching associated name. Like so:

    It is very slow, taking about a second (!!!!) to work, but it will do the job for now.

    String name = null;
    
        switch (reqCode) {
            case (REQUEST_CONTACT_PICKER):
                if (resultCode == Activity.RESULT_OK) {
                    Uri contactData = data.getData();
                    Cursor c = getContentResolver().query(contactData, null, null, null, null);
                    if (c.moveToFirst()) {
                        name = c.getString(c.getColumnIndex(Phone.DISPLAY_NAME));
                    }
                }
                break;
        }
    
        if (reqCode == REQUEST_CONTACT_PICKER) {
            if (resultCode == Activity.RESULT_OK) {
                if (data != null) {
                    Uri contactData = data.getData();
                    ArrayList <String> arrayList = new ArrayList<>();
    
                    try {
    
                        ContentResolver cr = getContentResolver();
                        Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
                        if (cur.getCount() > 0) {
                            while (cur.moveToNext()) {
                                String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
                                String nameCheck = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
                                //Log.d("NAMES", "name: " + name + "... nameCom: " + nameCheck);
                                if(!nameCheck.equals(name))continue;
                                Log.d("SUCCESS", "Name Chosen");
                                if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
                                    Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                                            null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?",
                                            new String[]{id}, null);
                                    while (pCur.moveToNext()) {
                                        int phoneType = pCur.getInt(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE));
                                        String phoneNumber = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                                        StringBuilder sb = new StringBuilder("Range of Numbers: ");
                                        switch (phoneType) {
                                            case Phone.TYPE_MOBILE:
                                                arrayList.add("Cell Number: " + phoneNumber);
                                                sb.append("MOBILE: " + phoneNumber + "; ");
                                                Log.e(name + "(mobile number)", phoneNumber);
                                                break;
                                            case Phone.TYPE_HOME:
                                                arrayList.add("Home Number: " + phoneNumber);
                                                sb.append("HOME: " + phoneNumber + "; ");
                                                Log.e(name + "(home number)", phoneNumber);
                                                break;
                                            case Phone.TYPE_WORK:
                                                arrayList.add("Work Number: " + phoneNumber);
                                                sb.append("WORK: " + phoneNumber + "; ");
                                                Log.e(name + "(work number)", phoneNumber);
                                                break;
                                            case Phone.TYPE_OTHER:
                                                arrayList.add("Misc Number: " + phoneNumber);
                                                sb.append("OTHER: " + phoneNumber + "; ");
                                                Log.e(name + "(other number)", phoneNumber);
                                                break;
                                            default:
                                                break;
                                        }
                                        //Toast.makeText(getBaseContext(), sb.toString(), Toast.LENGTH_SHORT).show();
                                    }
                                    pCur.close();
    
    
                                    if(arrayList.isEmpty()){
                                        Toast.makeText(getBaseContext(), "No Phone Number Found", Toast.LENGTH_LONG).show();
                                    } else if(arrayList.size() == 1){
                                        makeNewTxtAlarm(arrayList.get(0), name);
                                    } else if (arrayList.size() > 1){
                                        selectNewTxtAlarmNumber(arrayList, name);
                                    } else{
                                        Toast.makeText(getBaseContext(), "There is a problem with this contact...", Toast.LENGTH_LONG).show();
                                    }
                                }
                            }
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                        Toast.makeText(getBaseContext(), "There is a problem with this contact...", Toast.LENGTH_LONG).show();
                        Log.d("FILES", "Failed to get phone data");
                    }
                }
            }
        }