Android 联系人数据库给了我一个例外
每当我尝试访问联系人的号码时,都会出现索引越界异常,任何人都可以告诉我我在这里做错了什么。
类的成员
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试
在 while 循环之前添加,并将
while(){}
更改为do{} while()
edit: 对
电话
光标:添加。
在 try{} 语句之前
Try adding
before your while loop, and changing
while(){}
todo{} while()
edit: do the same with
phones
cursor:add
before your try{} statement.