Android 联系人数据库给了我一个例外

发布于 2024-12-10 01:15:15 字数 4426 浏览 0 评论 0原文

每当我尝试访问联系人的号码时,都会出现索引越界异常,任何人都可以告诉我我在这里做错了什么。

类的成员

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

在我的 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();
            }
        }
    }

堆栈跟踪中

/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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

转身泪倾城 2024-12-17 01:15:15

尝试

cur.moveToFirst(); 

在 while 循环之前添加,并将 while(){} 更改为 do{} while()

edit:电话光标:
添加。

phone.moveToFirst();

在 try{} 语句之前

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文