0

我想与联系人分开获取名字和姓氏。

但是当我使用以下过程时,程序出现运行时错误。

Unfortunately, XXX has stopped

问题出在哪里?

private void PrintContacts(Cursor c){
    if(c.moveToFirst()){
        do{
            String contactID = c.getString(c.getColumnIndex(ContactsContract.Contacts._ID));
            String contactGivenName = c.getString(c.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME));
            Toast.makeText(this, contactGivenName, Toast.LENGTH_SHORT).show();
        }while(c.moveToNext());
    }
}

日志错误:

08-17 12:02:43.750: ERROR/AndroidRuntime(21049): FATAL EXCEPTION: main
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mhb.Provider/com.mhb.Provider.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:2110)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2135)
    at android.app.ActivityThread.access$700(ActivityThread.java:140)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1237)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:137)
    at android.app.ActivityThread.main(ActivityThread.java:4921)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:511)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1027)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794)
    at dalvik.system.NativeStart.main(Native Method)
    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:114)
    at com.mhb.Provider.MainActivity.PrintContacts(MainActivity.java:62)
    at com.mhb.Provider.MainActivity.onCreate(MainActivity.java:54)
    at android.app.Activity.performCreate(Activity.java:5206)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2074)
    ... 11 more

-------------------------------------------------- -----

代码的其他部分:

    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    Uri allContacts = ContactsContract.Contacts.CONTENT_URI;

    Cursor c;
    if (android.os.Build.VERSION.SDK_INT <11) {
        c = managedQuery(allContacts, null, null, null, null);
    } else {
        CursorLoader cursorLoader = new CursorLoader(this, allContacts, null, null, null, null);
        c = cursorLoader.loadInBackground();
    }

    String[] columns = new String[] {
            ContactsContract.Contacts.DISPLAY_NAME,
            ContactsContract.Contacts._ID
    };

    int[] views = new int[] {R.id.contactName, R.id.contactID};

    SimpleCursorAdapter adapter;

    if(Build.VERSION.SDK_INT <11){
        adapter = new SimpleCursorAdapter(this, R.layout.main, c, columns, views);
    } else {
        adapter = new SimpleCursorAdapter(this, R.layout.main, c, columns, views,
                CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
    }

    this.setListAdapter(adapter);
    PrintContacts(c);
}
4

0 回答 0