1
public void onClick(View v) {

    switch (v.getId()) {
    int record_position = null;
    case R.id.Button01:
        // Get all contacts
        db.open();
        Cursor c = db.getAllContacts();
        if (record_position == null){
            if (c.moveToFirst()) {
                record_position = c.getPosition();
                DisplayContact(c);
            }
        }
        else if (c.moveToPosition(record_position)){     
            c.moveToNext();
            record_position = c.getPosition();
            DisplayContact(c);
        }
        else {
            if(c.moveToFirst){
                record_position = c.getPosition();
                DisplayContact(c);
            }
        }
        db.close();
        break;
    }
}

Why do i get this error? What is the problem? What is going wrong?Why do i get this error? What is the problem? What is going wrong?

2
  • In the future post the error. Commented May 25, 2013 at 8:01
  • It's a compilation error. He is writing code in switch block rather than in the case block.
    – Sam
    Commented May 25, 2013 at 9:01

2 Answers 2

1

You can't declare a variable before the first case block.

Move the variable deceleration to be above the switch.

1

Move the variable out of the case switch:

int record_position = null;
...
public void onClick(View v) {


    switch (v.getId()) {

    case R.id.Button01:
        // Get all contacts
        db.open();
        Cursor c = db.getAllContacts();
        if (record_position == null){
            if (c.moveToFirst()) {
                record_position = c.getPosition();
                DisplayContact(c);
            }
        }
        else if (c.moveToPosition(record_position)){     
            c.moveToNext();
            record_position = c.getPosition();
            DisplayContact(c);
        }
        else {
            if(c.moveToFirst){
                record_position = c.getPosition();
                DisplayContact(c);
            }
        }
        db.close();
        break;
    }
}
0

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.