package com.Kiro.Test;
import android.app.Activity;
import android.content.ContentResolver;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.Contacts.People;
public class TestActivity extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
setContentView(R.layout.main);
Uri uri=People.CONTENT_URI;
String projection[]=new String[]{People._ID,People.NAME,People.NUMBER};
Cursor cur=this.managedQuery(uri, projection, null, null, null);
int id=cur.getColumnIndex(People._ID);
super.onCreate(savedInstanceState);
do{
System.out.print(cur.getString(id));
System.out.println("");
}while(cur.moveToNext());
}
}
当我在我的模拟器中运行此代码时,logcat找到 CursorIndexoutofBoundsException 你能帮我解决这个问题吗?
答案 0 :(得分:1)
似乎问题在这里:
do{
System.out.print(cur.getString(id));
System.out.println("");
}while(cur.moveToNext());
你需要先调用cur.moveToFirst()将光标设置在第0位:
cur.moveToFirst();
while(cur.hasNext(){
cur.moveToNext();
System.out.print(cur.getString(id));
System.out.println("");
}
答案 1 :(得分:0)
而不是使用“do while”尝试使用while,因为do至少运行一次,即使条件为false。
while(cur.moveToNext()){
System.out.print(cur.getString(id));
System.out.println("");
}
我的问题就在于此。