0
votes

i try to get contact numbers and names. I'm gettin names correctly but when i try to get the numbers i getting error.

here is the code :

public void onActivityResult(int reqCode, int resultCode, Intent data) {
    super.onActivityResult(reqCode, resultCode, data);

    ArrayList<String> contactList = new ArrayList<String>();

    switch (reqCode) {
    case (0):
        if (resultCode == Activity.RESULT_OK) {
            Uri contactData = data.getData();
            Cursor c = getContentResolver().query(contactData, null, null,
                    null, null);
            if (c.moveToFirst()) {
                String name = c
                        .getString(c
                                .getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));


                String phoneNumber = c.getString(c
                        .getColumnIndex(Phone.NUMBER));

                String contact = name + "" + phoneNumber;

                contactList.add(contact);
            }
        }
        break;
    }

here is the logCat :

08-12 16:25:19.790: E/WindowManager(14381): Activity tr.com.turkcell.shmobile.MainActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@421c0090 that was originally added here

08-12 16:25:19.790: E/WindowManager(14381): android.view.WindowLeaked: Activity tr.com.turkcell.shmobile.MainActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@421c0090 that was originally added here 08-12 16:25:19.790: E/WindowManager(14381): at android.view.ViewRootImpl.(ViewRootImpl.java:374) 08-12 16:25:19.790: E/WindowManager(14381): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:292) 08-12 16:25:19.790: E/WindowManager(14381): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:224) 08-12 16:25:19.790: E/WindowManager(14381): at android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java:149) 08-12 16:25:19.790: E/WindowManager(14381): at android.view.Window$LocalWindowManager.addView(Window.java:549) 08-12 16:25:19.790: E/WindowManager(14381): at android.app.Dialog.show(Dialog.java:277) 08-12 16:25:19.790: E/WindowManager(14381): at android.app.ProgressDialog.show(ProgressDialog.java:116) 08-12 16:25:19.790: E/WindowManager(14381): at android.app.ProgressDialog.show(ProgressDialog.java:99) 08-12 16:25:19.790: E/WindowManager(14381): at android.app.ProgressDialog.show(ProgressDialog.java:94) 08-12 16:25:19.790: E/WindowManager(14381): at tr.com.turkcell.shmobile.MainActivity$GetProduts.onPreExecute(MainActivity.java:188) 08-12 16:25:19.790: E/WindowManager(14381): at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:586) 08-12 16:25:19.790: E/WindowManager(14381): at android.os.AsyncTask.execute(AsyncTask.java:534) 08-12 16:25:19.790: E/WindowManager(14381): at tr.com.turkcell.shmobile.MainActivity$1$1.run(MainActivity.java:59) 08-12 16:25:19.790: E/WindowManager(14381): at android.app.Activity.runOnUiThread(Activity.java:4591) 08-12 16:25:19.790: E/WindowManager(14381): at tr.com.turkcell.shmobile.MainActivity$1.onClick(MainActivity.java:56) 08-12 16:25:19.790: E/WindowManager(14381): at android.view.View.performClick(View.java:4084) 08-12 16:25:19.790: E/WindowManager(14381): at android.view.View$PerformClick.run(View.java:16966) 08-12 16:25:19.790: E/WindowManager(14381): at android.os.Handler.handleCallback(Handler.java:615) 08-12 16:25:19.790: E/WindowManager(14381): at android.os.Handler.dispatchMessage(Handler.java:92) 08-12 16:25:19.790: E/WindowManager(14381): at android.os.Looper.loop(Looper.java:137) 08-12 16:25:19.790: E/WindowManager(14381): at android.app.ActivityThread.main(ActivityThread.java:4754) 08-12 16:25:19.790: E/WindowManager(14381): at java.lang.reflect.Method.invokeNative(Native Method) 08-12 16:25:19.790: E/WindowManager(14381): at java.lang.reflect.Method.invoke(Method.java:511) 08-12 16:25:19.790: E/WindowManager(14381): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833) 08-12 16:25:19.790: E/WindowManager(14381): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600) 08-12 16:25:19.790: E/WindowManager(14381): at dalvik.system.NativeStart.main(Native Method) 08-12 16:25:21.370: E/CursorWindow(14381): Failed to read row 0, column -1 from a CursorWindow which has 1 rows, 29 columns. 08-12 16:25:21.370: D/AndroidRuntime(14381): Shutting down VM 08-12 16:25:21.370: W/dalvikvm(14381): threadid=1: thread exiting with uncaught exception (group=0x41b6a318) 08-12 16:25:21.380: E/AndroidRuntime(14381): FATAL EXCEPTION: main 08-12 16:25:21.380: E/AndroidRuntime(14381): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=0, result=-1, data=Intent { dat=content://com.android.contacts/contacts/lookup/209i1510398b8e8d8617/1 flg=0x1 }} to activity {tr.com.turkcell.shmobile/tr.com.turkcell.shmobile.IndexActivity}: java.lang.IllegalStateException: Couldn't read row 0, col -1 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it. 08-12 16:25:21.380: E/AndroidRuntime(14381): at android.app.ActivityThread.deliverResults(ActivityThread.java:3150) 08-12 16:25:21.380: E/AndroidRuntime(14381): at android.app.ActivityThread.handleSendResult(ActivityThread.java:3193) 08-12 16:25:21.380: E/AndroidRuntime(14381): at android.app.ActivityThread.access$1100(ActivityThread.java:130) 08-12 16:25:21.380: E/AndroidRuntime(14381): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1243) 08-12 16:25:21.380: E/AndroidRuntime(14381): at android.os.Handler.dispatchMessage(Handler.java:99) 08-12 16:25:21.380: E/AndroidRuntime(14381): at android.os.Looper.loop(Looper.java:137) 08-12 16:25:21.380: E/AndroidRuntime(14381): at android.app.ActivityThread.main(ActivityThread.java:4754) 08-12 16:25:21.380: E/AndroidRuntime(14381): at java.lang.reflect.Method.invokeNative(Native Method) 08-12 16:25:21.380: E/AndroidRuntime(14381): at java.lang.reflect.Method.invoke(Method.java:511) 08-12 16:25:21.380: E/AndroidRuntime(14381): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833) 08-12 16:25:21.380: E/AndroidRuntime(14381): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600) 08-12 16:25:21.380: E/AndroidRuntime(14381): at dalvik.system.NativeStart.main(Native Method) 08-12 16:25:21.380: E/AndroidRuntime(14381): Caused by: java.lang.IllegalStateException: Couldn't read row 0, col -1 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it. 08-12 16:25:21.380: E/AndroidRuntime(14381): at android.database.CursorWindow.nativeGetString(Native Method) 08-12 16:25:21.380: E/AndroidRuntime(14381): at android.database.CursorWindow.getString(CursorWindow.java:434) 08-12 16:25:21.380: E/AndroidRuntime(14381): at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:51) 08-12 16:25:21.380: E/AndroidRuntime(14381): at android.database.CursorWrapper.getString(CursorWrapper.java:114) 08-12 16:25:21.380: E/AndroidRuntime(14381): at tr.com.turkcell.shmobile.IndexActivity.onActivityResult(IndexActivity.java:85) 08-12 16:25:21.380: E/AndroidRuntime(14381): at android.app.Activity.dispatchActivityResult(Activity.java:5192) 08-12 16:25:21.380: E/AndroidRuntime(14381): at android.app.ActivityThread.deliverResults(ActivityThread.java:3146) 08-12 16:25:21.380: E/AndroidRuntime(14381): ... 11 more 08-12 16:25:21.410: D/dalvikvm(14381): GC_CONCURRENT freed 2769K, 32% free 6571K/9543K, paused 2ms+4ms, total 37ms

1
what error u r getting?Android Killer
Does every contact have a number? Is it a NPE?iaindownie
yes every contact has a numbermstfdz
come on man if not NPE, then what it is ?Android Killer
İllegalStateException !!mstfdz

1 Answers

0
votes

you need 2 cursors to get a contact number , why? .. well suppose a contact got more than one number what you will do :)? .... so you need a cursor to go through the contacts and a cursor to go through the contact number's .. something like @Piyush said and this

ContentResolver cr = getContentResolver();

        Cursor cursor = cr.query(uri, projection, selection, null, sortOrder);
    while(cursor.moveToNext()){//this loops through contacts and put them in object ContactTest which i already made
        ContactTest c1 = new ContactTest();
        c1.setContactName(cursor.getString(/*name index*/));
        c1.setContactID(cursor.getString(/*id index you will need it*/));
        Cursor pCursor = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[] {c1.getContactID()}, null);

        while(pCursor.moveToNext()){ // this loops through the contact number's

            c1.setContactPhone(pCursor.getString(pCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)));
        }
    }