0
public class TestActivity extends Activity{

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    ContentResolver cr=getContentResolver();
    Cursor phone=cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);
    while(phone.moveToNext())
    {
        String name=phone.getString(phone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
        Log.d("name:", name);
        get(name);
    }phone.close();
}

private void get(String name) {
    DatabaseHandler db=new DatabaseHandler(getApplicationContext());
    Cursor c=db.returnFriend(name);
    Log.d("get", c.getString(0));

}


 }

   //Databse Handler Class query
public Cursor returnFriendList(String names)
{
    SQLiteDatabase db=this.getReadableDatabase();
    return db.query(TABLE_CONTACT, new String[]{name}, name+"=?", new String[] { String.valueOf(names) },null, null,null);


}

Stacktrace:

02-27 01:15:58.489: E/AndroidRuntime(32140): FATAL EXCEPTION: main
02-27 01:15:58.489: E/AndroidRuntime(32140): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.androidhive/com.example.androidhive.TestActivity}: android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 0
02-27 01:15:58.489: E/AndroidRuntime(32140):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1955)
02-27 01:15:58.489: E/AndroidRuntime(32140):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
02-27 01:15:58.489: E/AndroidRuntime(32140):    at android.app.ActivityThread.access$600(ActivityThread.java:122)
02-27 01:15:58.489: E/AndroidRuntime(32140):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
02-27 01:15:58.489: E/AndroidRuntime(32140):    at android.os.Handler.dispatchMessage(Handler.java:99)
02-27 01:15:58.489: E/AndroidRuntime(32140):    at android.os.Looper.loop(Looper.java:137)
02-27 01:15:58.489: E/AndroidRuntime(32140):    at android.app.ActivityThread.main(ActivityThread.java:4340)
02-27 01:15:58.489: E/AndroidRuntime(32140):    at java.lang.reflect.Method.invokeNative(Native Method)
02-27 01:15:58.489: E/AndroidRuntime(32140):    at java.lang.reflect.Method.invoke(Method.java:511)
02-27 01:15:58.489: E/AndroidRuntime(32140):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
02-27 01:15:58.489: E/AndroidRuntime(32140):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
02-27 01:15:58.489: E/AndroidRuntime(32140):    at dalvik.system.NativeStart.main(Native Method)
02-27 01:15:58.489: E/AndroidRuntime(32140): Caused by: android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 0
02-27 01:15:58.489: E/AndroidRuntime(32140):    at android.database.AbstractCursor.checkPosition(AbstractCursor.java:434)
02-27 01:15:58.489: E/AndroidRuntime(32140):    at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:136)
02-27 01:15:58.489: E/AndroidRuntime(32140):    at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:50)
02-27 01:15:58.489: E/AndroidRuntime(32140):    at com.example.androidhive.TestActivity.get(TestActivity.java:33)
02-27 01:15:58.489: E/AndroidRuntime(32140):    at com.example.androidhive.TestActivity.onCreate(TestActivity.java:26)
02-27 01:15:58.489: E/AndroidRuntime(32140):    at android.app.Activity.performCreate(Activity.java:4465)
02-27 01:15:58.489: E/AndroidRuntime(32140):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
02-27 01:15:58.489: E/AndroidRuntime(32140):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1919)
02-27 01:15:58.489: E/AndroidRuntime(32140):    ... 11 more
3
  • 1
    cursor does not point to the first row Commented Feb 20, 2014 at 18:59
  • i have done so then to its shoiwing error that the cursoir is out of bound Commented Feb 20, 2014 at 19:03
  • Can you post returnFriend method? Commented Feb 20, 2014 at 19:21

2 Answers 2

3
Cursor c=db.returnFriend(name);
Log.d("get", c.getString(0));

You don't moveToFirst before you start acting on the cursor, therefore it is placed at -1.

Sign up to request clarification or add additional context in comments.

2 Comments

after writting move to first also it show error that cursor is out of bound
@user3181391 Yes, because the cursor is empty. Check the return value of moveToFirst() to see whether the cursor points to a row after the move.
-1

Change the below,

ContentResolver cr=getContentResolver();
Cursor phone=cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);
if(phone!=null){
   **phone.moveToFirst();**
   do
   {
        String name = phone.getString(phone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
        Log.d("name:", name);
        get(name);
    }while(phone.moveToNext());
    phone.close();
}

phone.moveToFirst() is where you make your cursor point the first row of data available and then continue reading from there using moveToNext();

5 Comments

This will skip the first row and does not address the actual problem elsewhere.
@laalto - Didn't get you, could you please explain?
moveToFirst() followed by moveToNext() will skip the first result row. This isn't the OP's problem,it's the other cursor, njzk2 has it right.
FYI, it still doesn't solve OP's problem. Now it just doesn't introduce new problems :)
So will it still give an error? I understand njzk2's answer but how is this different? Can you throw some light?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.