• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps AddressBook displaying Contact's number

personxd1

Lurker
Jan 7, 2017
4
0
I'm relatively new to Android, and I'm having trouble figuring out how to read my contact's phone number. My code below reads the contact's name and places it into an address book (not in alphabetical order). What I have to do is create an address book with contacts and their phone number in the same line.
Code:
import android.app.ListActivity;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.widget.ArrayAdapter;

public class MainActivity extends ListActivity {

   @Override
   public void onCreate(Bundle savedInstanceState) {

      super.onCreate(savedInstanceState);
        Log.i("TurnToTech", "Project name - AddressBook");

Cursor cursor = getContentResolver().query(
            ContactsContract.Contacts.CONTENT_URI, null, null, null, null);

      ArrayAdapter<String> list = new ArrayAdapter<String>(this,
            R.layout.activity_main)

      while (cursor.moveToNext()) {

         String name = cursor.getString(cursor
               .getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));

           // I checked online and this is what is used to get the contact's number
          // But this crashes my AndressBook app.
            String phone = cursor.getString(cursor
                    .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));

         list.add(name);
      }

      setListAdapter(list);

   }

Any insight would be appreciated!
 
Code:
01/07 15:57:00: Launching app
Cold swapped changes.
$ adb shell am start -n "org.turntotech.addressbook/org.turntotech.addressbook.MainActivity" -a android.intent.action.MAIN -c android.intent.category.LAUNCHER -D
Waiting for application to come online: org.turntotech.addressbook | org.turntotech.addressbook.test
Waiting for application to come online: org.turntotech.addressbook | org.turntotech.addressbook.test
Connecting to org.turntotech.addressbook
W/ActivityThread: Application org.turntotech.addressbook is waiting for the debugger on port 8100...
I/System.out: Sending WAIT chunk
I/art: Debugger is active
I/System.out: Debugger has connected
I/System.out: waiting for debugger to settle...
I/System.out: waiting for debugger to settle...
Connected to the target VM, address: 'localhost:8618', transport: 'socket'
I/System.out: waiting for debugger to settle...
I/System.out: waiting for debugger to settle...
I/System.out: waiting for debugger to settle...
I/System.out: waiting for debugger to settle...
I/System.out: waiting for debugger to settle...
I/System.out: waiting for debugger to settle...
I/System.out: waiting for debugger to settle...
I/System.out: debugger has settled (1458)
W/System: ClassLoader referenced unknown path: /data/app/org.turntotech.addressbook-1/lib/x86
I/InstantRun: Instant Run Runtime started. Android package is org.turntotech.addressbook, real application class is null.
W/System: ClassLoader referenced unknown path: /data/app/org.turntotech.addressbook-1/lib/x86
I/TurnToTech: Project name - AddressBook
E/CursorWindow: Failed to read row 0, column -1 from a CursorWindow which has 4 rows, 36 columns.
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
                  Process: org.turntotech.addressbook, PID: 9551
                  java.lang.RuntimeException: Unable to start activity ComponentInfo{org.turntotech.addressbook/org.turntotech.addressbook.MainActivity}: java.lang.IllegalStateException: Couldn't read row 0, col -1 from CursorWindow.  Make sure the Cursor is initialized correctly before accessing data from it.
                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2665)
                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
                      at android.app.ActivityThread.-wrap12(ActivityThread.java)
                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
                      at android.os.Handler.dispatchMessage(Handler.java:102)
                      at android.os.Looper.loop(Looper.java:154)
                      at android.app.ActivityThread.main(ActivityThread.java:6119)
                      at java.lang.reflect.Method.invoke(Native Method)
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
                   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.
                      at android.database.CursorWindow.nativeGetString(Native Method)
                      at android.database.CursorWindow.getString(CursorWindow.java:438)
                      at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:51)
                      at android.database.CursorWrapper.getString(CursorWrapper.java:137)
                      at org.turntotech.addressbook.MainActivity.onCreate(MainActivity.java:33)
                      at android.app.Activity.performCreate(Activity.java:6679)
                      at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2618)
                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726) 
                      at android.app.ActivityThread.-wrap12(ActivityThread.java) 
                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477) 
                      at android.os.Handler.dispatchMessage(Handler.java:102) 
                      at android.os.Looper.loop(Looper.java:154) 
                      at android.app.ActivityThread.main(ActivityThread.java:6119) 
                      at java.lang.reflect.Method.invoke(Native Method) 
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) 
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776) 
Disconnected from the target VM, address: 'localhost:8618', transport: 'socket'
I copied this from my console.
 
Upvote 0
What's the value of this

Code:
ContactsContract.CommonDataKinds.Phone.NUMBER

Reason for asking is that this error

Code:
Failed to read row 0, column -1

Has a suspicious value of -1 for the column number.

What I would do is run the app in debug mode, and set a breakpoint at this code. Look at values such as this and it may give you more insight into what's causing the problem.
 
Last edited by a moderator:
  • Like
Reactions: sweetndreemy73
Upvote 0

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones