0

I have the following code

        image1.setOnClickListener(new View.OnClickListener() {

        int randInt = new Random().nextDouble() < 0.5 ? 1 : 2;

        if (randInt.equals(1)) {
            public void onClick(View view) {
                if (isFirstImage) {       
                    applyRotation(0, 90);
                    applyRotation(0, 90);
                    isFirstImage = !isFirstImage;

                } else {    
                    applyRotation(0, -90);
                    applyRotation(0, -90);
                    isFirstImage = !isFirstImage;
                }
            }
        } else if (randInt.equals(2)) {
            public void onClick(View view) {
                if (isFirstImage) {       
                    applyRotation(0, 90);
                    applyRotation(0, 90);
                    applyRotation(0, 90);

                    isFirstImage = !isFirstImage;

                } else {    
                    applyRotation(0, -90);
                    applyRotation(0, -90);
                    applyRotation(0, -90);
                    isFirstImage = !isFirstImage;
                }
            }
        }

    }); 

I have a "Syntax error, insert ";" to complete Statement" on the line where I declare my integer, when I clearly do have a ";" there. I have a few "Syntax error on token "(",:expected" and "Syntax error on token ")",;expected" where I have "public void onClick(View view) {" I have a "Syntax error, insert "}" to complete Statement" but I looked everywhere and it seems I have closed all my statements.

I think Eclipse is giving me false errors, and I tried Project > Clean, but that didn't solve it. Please help, thanks!

1
  • you cannot call .equals on an int. just use == Commented Apr 26, 2013 at 19:55

1 Answer 1

4

I think the missing ";" error is spurious. Your real problem is how you are trying to declare the onClick listeners. The if blocks cannot contain method declarations like that. Try the following:

image1.setOnClickListener(new View.OnClickListener() {

    int randInt = new Random().nextDouble() < 0.5 ? 1 : 2;

    @Override
    public void onClick(View view) {
        if (randInt == 1) {
            if (isFirstImage) {       
                applyRotation(0, 90);
                applyRotation(0, 90);
            } else {    
                applyRotation(0, -90);
                applyRotation(0, -90);
            }
        } else if (randInt == 2) {
            if (isFirstImage) {       
                applyRotation(0, 90);
                applyRotation(0, 90);
                applyRotation(0, 90);
            } else {    
                applyRotation(0, -90);
                applyRotation(0, -90);
                applyRotation(0, -90);
            }
        }
        isFirstImage = !isFirstImage;
    }
});

This will fix randInt at the time the OnClickListener is attached to image1. If you want a random rotation each time image1 is clicked, move the declaration of randInt to be the first statement of the onClick method itself.

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

4 Comments

he probably wants to call nextDouble() inside each onClick(), otherwise the same randInt will be used for every click right?
@StevenByle - Good point; I'll add it to my answer. I don't know what OP actually wants to accomplish.
If the number is 1, I want it to rotate twice, but if the number is 2, I want it to rotate three times, it's not really working though; the image is only flipping once even though I'm calling the "applyRotation(0,90)" twice.
@MostafaSaadat - What does applyRotation do? (By the way, rotating by 90 three times is the same as rotating by -90 once, and vice versa.) The issue of where to declare randInt (as a field of the anonymous OnClickListener class or as a local variable of the onClick method) depends on whether you want the behavior to be the same on every click (use a field) or whether you want each click to be independently random (use a local variable).

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.