当我检索联系人时,它会给出非法状态异常..
这是我的代码..
private ArrayList<HashMap<String, String>> getContacts() {
// TODO Auto-generated method stub
Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
String[] projection = new String[] {ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
ContactsContract.CommonDataKinds.Phone.NUMBER};
Cursor people = getContentResolver().query(uri, projection, null, null, null);
int indexName = people.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
int indexNumber = people.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
int numberType = people.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE);
if(people.getCount()>0){
while (people.moveToNext())
{
// For Number Type
String nameType="";
int type = people.getInt(numberType);
switch (type) {
case Phone.TYPE_HOME:
nameType= "_Home";
break;
case Phone.TYPE_MOBILE:
nameType= "_Mobile";
break;
case Phone.TYPE_WORK:
nameType= "_Work";
break;
case Phone.TYPE_OTHER:
nameType= "_Other";
break;
}
String name = people.getString(indexName)+""+nameType;
String number = people.getString(indexNumber);
System.out.println("name: "+name+" number: "+number);
// Do work...
HashMap<String, String> namePhoneType = new HashMap<String, String>();
namePhoneType.put(name, number);
System.out.println("Hash map: "+namePhoneType);
//namePhoneType.put("Name", name);
//namePhoneType.put("Phone", number);
// adding to array list
contactList.add(namePhoneType);
System.out.println("Array list: "+contactList);
}
}
return contactList;
}
这是我的日志...
01-27 21:10:29.290: E/AndroidRuntime(333): FATAL EXCEPTION: main
01-27 21:10:29.290: E/AndroidRuntime(333): java.lang.RuntimeException: Unable to start activityComponentInfo{com.first/com.first.SecondActivity}: java.lang.IllegalStateException: get fieldslot from row 0 col -1 failed
01-27 21:10:29.290: E/AndroidRuntime(333): atandroid.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
01-27 21:10:29.290: E/AndroidRuntime(333): atandroid.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
01-27 21:10:29.290: E/AndroidRuntime(333): atandroid.app.ActivityThread.access$1500(ActivityThread.java:117)
01-27 21:10:29.290: E/AndroidRuntime(333): atandroid.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
01-27 21:10:29.290: E/AndroidRuntime(333): atandroid.os.Handler.dispatchMessage(Handler.java:99)
01-27 21:10:29.290: E/AndroidRuntime(333): at android.os.Looper.loop(Looper.java:123)
01-27 21:10:29.290: E/AndroidRuntime(333): atandroid.app.ActivityThread.main(ActivityThread.java:3683)
01-27 21:10:29.290: E/AndroidRuntime(333): at java.lang.reflect.Method.invokeNative(Native Method)
01-27 21:10:29.290: E/AndroidRuntime(333): at java.lang.reflect.Method.invoke(Method.java:507)
01-27 21:10:29.290: E/AndroidRuntime(333): atcom.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
01-27 21:10:29.290: E/AndroidRuntime(333): atcom.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
01-27 21:10:29.290: E/AndroidRuntime(333): at dalvik.system.NativeStart.main(Native Method)
01-27 21:10:29.290: E/AndroidRuntime(333): Caused by: java.lang.IllegalStateException: get fieldslot from row 0 col -1 failed
01-27 21:10:29.290: E/AndroidRuntime(333): atandroid.database.CursorWindow.getLong_native(Native Method)
01-27 21:10:29.290: E/AndroidRuntime(333): atandroid.database.CursorWindow.getInt(CursorWindow.java:434)
01-27 21:10:29.290: E/AndroidRuntime(333): atandroid.database.AbstractWindowedCursor.getInt(AbstractWindowedCursor.java:93)
01-27 21:10:29.290: E/AndroidRuntime(333): atandroid.database.CursorWrapper.getInt(CursorWrapper.java:123)
01-27 21:10:29.290: E/AndroidRuntime(333): atcom.first.SecondActivity.getContacts(SecondActivity.java:78)
01-27 21:10:29.290: E/AndroidRuntime(333): atcom.first.SecondActivity.onCreate(SecondActivity.java:59)
01-27 21:10:29.290: E/AndroidRuntime(333): atandroid.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
01-27 21:10:29.290: E/AndroidRuntime(333): atandroid.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
答案 0 :(得分:0)
请在此处查看错误代码:
String[] projection = new String[] {ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
ContactsContract.CommonDataKinds.Phone.NUMBER};
Cursor people = getContentResolver().query(uri, projection, null, null, null);
int indexName = people.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
int indexNumber = people.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
int numberType = people.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE);
在前两行中,您有一个Cursor
:people
,其中包含两个字段:DISPLAY_NAME
和NUMBER
。在查询之后,您尝试获取TYPE
并返回-1,因为您的cursor
仅包含2个字段(不是TYPE),因此这是您的错误。
P / s:在query
之后,您应该使用cursor.moveToFirst()
功能。为什么?看Android Cursor link