android studio单击按钮崩溃西蒙说

时间:2019-01-03 19:14:47

标签: android-studio button onclicklistener

因此,我正在制作simon所说的游戏,但我无法让用户单击颜色,以便游戏可以查看用户单击的内容是否正确。

现在,我已经为按钮实现了onclick方法。目的是当用户单击按钮一定次数时,游戏将检查显示的图案是否与用户提供的图案相同。游戏显示出良好的图案。但是,在游戏显示了模式之后单击按钮会导致游戏停止工作。我尝试运行调试器,但是当我执行该操作时,该活动将无法加载,因此我实际上无法单击该按钮。但是当我刚运行游戏时,活动显示,但是当我单击按钮时崩溃。

这是创建时的

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

int[] pattern;
int[] guess;
Button[] colours;
public int count;
public TextView Final;
public int score;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    Button green =  findViewById(R.id.Green);

    Button blue =  findViewById(R.id.Blue);

    Button red =  findViewById(R.id.Red);

    Button purple =  findViewById(R.id.purple);

    TextView Final = findViewById(R.id.Score);
    colours = new Button[]{green, blue, red, purple};

    int score = 0;
    do {
        pattern = showPattern(colours, score);
        guess = getGuess(colours, score);
    }while(Arrays.equals(pattern,guess));
    Final.setText("Score is" + score);
}

这是onclick方法:

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.Green:
                guess[count] = 0;
                count++;
                if (count >= score + 1 && Arrays.equals(pattern,guess)) {
                    score++;
                    pattern = showPattern(colours,score);
                    count = 0;
                }
                else if (count>=score + 1) {
                    String result = "You're score is " + score;
                    Final.setText(result);

                }

            case R.id.Blue:

                guess[count] = 1;
                count++;
                if (count >= score + 1 && Arrays.equals(pattern,guess)) {
                    score++;
                    pattern = showPattern(colours,score);
                    count = 0;
                }

                else if (count>=score + 1) {
                    String result = "You're score is " + score;
                    Final.setText(result);

                }

            case R.id.Red:

                guess[count] =2;
                count++;
                if (count >= score + 1 && Arrays.equals(pattern,guess)) {
                    score++;
                    pattern = showPattern(colours,score);
                    count = 0;
                }

                else if (count>=score + 1) {
                    String result = "You're score is " + score;
                    Final.setText(result);

                }


            case R.id.purple:
                guess[count] = 3;
                count++;
                if (count >= score + 1 && Arrays.equals(pattern,guess)) {
                    score++;
                    pattern = showPattern(colours,score);
                    count = 0;
                }
                else if (count>=score + 1) {
                    String result = "You're score is " + score;
                    Final.setText(result);

                }

            default:
                break;

        }

}

这是我认为导致我的应用出现问题的getGuess方法。

public int[] getGuess(Button[]  colours, int score) {
    guess = new int[score+1];
    colours[0].setOnClickListener(this);
    colours[1].setOnClickListener(this);
    colours[2].setOnClickListener(this);
    colours[3].setOnClickListener(this);

    while(count<score+1){

    }

    return guess;

这是showPattern方法:

public int[] showPattern(final Button[] colours, int score) {
    int[] pattern = new int[score + 1];
    Random rand = new Random();

    for (int j=0;j<score + 1;j++) {
        pattern[j] = rand.nextInt(4);



        switch (pattern[j]) {
            case 0:
                new CountDownTimer(2000,2000) {
                    public void onTick(long millisUntilFinished) {
                        colours[0].setBackgroundColor(getColor(R.color.lightGreen));
                    }


                    public void onFinish() {
                        colours[0].setBackgroundColor(getColor(R.color.green));
                    }
                }.start();
                break;



            case 1:
                new CountDownTimer(2000,2000) {
                    public void onTick(long millisUntilFinished) {
                        colours[1].setBackgroundColor(getColor(R.color.lightBlue));
                    }
                    public void onFinish() {
                        colours[1].setBackgroundColor(getColor(R.color.blue));
                    }
                }.start();
                break;

            case 2:
                new CountDownTimer(2000,2000) {
                    public void onTick(long millisUntilFinished) {
                        colours[2].setBackgroundColor(getColor(R.color.lightRed));
                    }
                    public void onFinish() {
                        colours[2].setBackgroundColor(getColor(R.color.red));
                    }
                }.start();
                break;


            case 3:
                new CountDownTimer(2000,2000) {
                    public void onTick(long millisUntilFinished) {
                        colours[3].setBackgroundColor(getColor(R.color.lightPurple));
                    }
                    public void onFinish() {
                        colours[3].setBackgroundColor(getColor(R.color.purple));
                    }
                }.start();
                break;
        }
    }
    return pattern;
}

我期望一旦公共变量计数等于超出的变量分数,并且猜测和模式数组都相等,那么我就可以显示模式并等待用户再次猜测。但是,一旦单击按钮,游戏就会停止。

(UPDATE)因此,我认为除了getguess方法之外,do while循环似乎也在弄乱我的代码。另外,logcat的最后一行读取为

Accessing hidden method Landroid/view/ViewGroup;->makeOptionalFitsSystemWindows()V (light greylist, reflection)

0 个答案:

没有答案