0

I'm getting the error "Syntax error, insert ";" to complete ReturnStatement" in the following code on the line "return int i;"

public class RecordActivity extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_record);

    int i = intent.getIntExtra(MainActivity.EXTRA_INDEX, 0);
    int j = i+1;
    final String[]names = getResources().getStringArray(R.array.names);

    final TextView nameView = (TextView) findViewById(R.id.vNames);
    String vName = names[i];
    nameView.setText(vName);

    Button nextButton = (Button) findViewById(R.id.nextButton);
    nextButton.setOnClickListener(new OnClickListener() {
        public static onClickView(View v) {
            i=i+1;
            return int i;
        }
        }
    );
    }
    //...
}

I am trying to iterate the index of an array on button click and so am getting the onClick method to return the index 'i' so I can put it into a looping statement. I've checked all brackets close and can't see where I would be missing the ';'. Any help on this would be appreciated.

2
  • also, your onClickView should be a void and not a (nothing), you should declare i outside the onCreate method to be able to access and modify it in the onClickListener, and you cannot return an int from this method (even if you could, you wouldn't receive it anywhere). Plus, incrementing i won't actually do anything from what is visible of your code. Commented Nov 5, 2012 at 14:05
  • other point : why a static method ? Commented Nov 5, 2012 at 14:06

4 Answers 4

1

Change this to:

public void onClickView(View v) {
    i=i+1;
}

because the return type is void, not int. See: View.OnClickListener

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

Comments

0

Declare int i in the activity, then in the onClick just increment it:

public class RecordActivity extends Activity {
  int i;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_record);

i = intent.getIntExtra(MainActivity.EXTRA_INDEX, 0);
int j = i+1;
final String[]names = getResources().getStringArray(R.array.names);

final TextView nameView = (TextView) findViewById(R.id.vNames);
String vName = names[i];
nameView.setText(vName);

Button nextButton = (Button) findViewById(R.id.nextButton);
nextButton.setOnClickListener(new OnClickListener() {
    public void onClickView(View v) {
        i=i+1;
    }
    }
);
}
//...

Comments

0

I would try not redeclaring the variable, like:

return i;

Comments

0

Have you tried changing return int i; to return i; and changing the return type from void to int? It doesn't makes sense to 'redeclare' i while trying to return it.

Also, I believe that the return type should be void, so just remove the return statement.

Comments

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.