简体   繁体   中英

Why am I getting these syntax errors?

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!

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM