简体   繁体   English

为什么会出现这些语法错误?

[英]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. 我有一些“令牌“(”,:expected“上的语法错误,以及”令牌“)”上的语法错误“; expected”,其中我有“ public void onClick(View view){”,我有一个“语法错误,插入“}”以完成声明”,但我四处张望,似乎已经关闭了所有声明。

I think Eclipse is giving me false errors, and I tried Project > Clean, but that didn't solve it. 我认为Eclipse给了我错误的错误,我尝试了Project> Clean,但是并不能解决问题。 Please help, thanks! 请帮忙,谢谢!

I think the missing ";" 我认为缺少“;” error is spurious. 错误是虚假的。 Your real problem is how you are trying to declare the onClick listeners. 您真正的问题是如何尝试声明onClick侦听器。 The if blocks cannot contain method declarations like that. if块不能包含这样的方法声明。 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 . 这将在将OnClickListener附加到image1时修复randInt 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. 如果您希望每次单击image1都随机旋转,请将randInt的声明移动为onClick方法本身的第一条语句。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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