Android contacts database giving me an exception

删除回忆录丶 提交于 2019-12-13 06:53:03

问题


Whenever I try to access the number of a contact I get an index out of bounds exception, can anyone please tell me what I am doing wrong here.

Members of the class

private String[] names;
private String[] number;

In my onCreate

ContentResolver cr = getContentResolver();
    Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
    if (cur.getCount() > 0) 
    {
        names = new String[cur.getCount()];
        number = new String[cur.getCount()];
        while (cur.moveToNext()) 
        {
            String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
            String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
            names[cur.getPosition()] = name;
           // number[cur.getPosition()] = name;
            if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
                Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ id,null, null);
                try{
                    number[cur.getPosition()] = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                }
                catch(Exception e)
                {
                    Log.d("Exception", e.toString());
                }
                phones.close();
            }
        }
    }

stack trace

/AndroidRuntime(10284): FATAL EXCEPTION: main
E/AndroidRuntime(10284): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.informationkinetics.SMSS/com.informationkinetics.SMSS.Contact}: android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 1
E/AndroidRuntime(10284):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2781)
E/AndroidRuntime(10284):    at android.app.ActivityThread.startActivityNow(ActivityThread.java:2621)
E/AndroidRuntime(10284):    at android.app.LocalActivityManager.moveToState(LocalActivityManager.java:127)
E/AndroidRuntime(10284):    at android.app.LocalActivityManager.startActivity(LocalActivityManager.java:339)
E/AndroidRuntime(10284):    at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:651)
E/AndroidRuntime(10284):    at android.widget.TabHost.setCurrentTab(TabHost.java:323)
E/AndroidRuntime(10284):    at android.widget.TabHost$2.onTabSelectionChanged(TabHost.java:129)
E/AndroidRuntime(10284):    at android.widget.TabWidget$TabClickListener.onClick(TabWidget.java:453)
E/AndroidRuntime(10284):    at android.view.View.performClick(View.java:2408)
E/AndroidRuntime(10284):    at android.view.View$PerformClick.run(View.java:8817)
E/AndroidRuntime(10284):    at android.os.Handler.handleCallback(Handler.java:587)
E/AndroidRuntime(10284):    at android.os.Handler.dispatchMessage(Handler.java:92)
E/AndroidRuntime(10284):    at android.os.Looper.loop(Looper.java:143)
E/AndroidRuntime(10284):    at android.app.ActivityThread.main(ActivityThread.java:4914)
E/AndroidRuntime(10284):    at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(10284):    at java.lang.reflect.Method.invoke(Method.java:521)
E/AndroidRuntime(10284):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
E/AndroidRuntime(10284):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
E/AndroidRuntime(10284):    at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime(10284): Caused by: android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 1
E/AndroidRuntime(10284):    at android.database.AbstractCursor.checkPosition(AbstractCursor.java:580)
E/AndroidRuntime(10284):    at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:214)
E/AndroidRuntime(10284):    at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:41)
E/AndroidRuntime(10284):    at android.database.CursorWrapper.getString(CursorWrapper.java:140)
E/AndroidRuntime(10284):    at com.informationkinetics.SMSS.Contact.onCreate(Contact.java:44)
E/AndroidRuntime(10284):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1065)
E/AndroidRuntime(10284):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2745)
E/AndroidRuntime(10284):    ... 18 more

回答1:


Try adding

cur.moveToFirst(); 

before your while loop, and changing while(){} to do{} while()

edit: do the same with phones cursor: add

phone.moveToFirst();

before your try{} statement.



来源:https://stackoverflow.com/questions/7794070/android-contacts-database-giving-me-an-exception

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!