如何从数组中随机选择一个项目并将位置存储在int中然后检查int值?

时间:2017-01-15 00:03:22

标签: java android arrays

所以我在我正在制作的应用程序中有一个java类,其中包含我随机选择的项目数组,并将所选项目的位置存储在 int 中然后我想要检查int的值但是当我运行应用程序时,我在手机上收到此消息:不幸的是,NAME OF APP已停止。当代码尝试在每个复选框中显示文本时(每个复选框的ID为easyLevel1Check),我收到错误。有关详细信息,请参阅下面的代码。

public class easyLevel1 extends AppCompatActivity {

    Random r = new Random();
    int ranNumReadyText = (r.nextInt(99999 - 10000) + 10000);
    int setOthers = (r.nextInt(99999 - 10000) + 10000);
    int timer = 10;
    int myCheck;
    Boolean isRunning = false;
    CountDownTimer time;

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

        final Button easyLevel1Val = (Button)findViewById(R.id.easyLevel1Val);
        final TextView easyLevel1Timer = (TextView)findViewById(R.id.easyLevel1Timer);
        final TextView easyLevel1ReadyText = (TextView)findViewById(R.id.easyLevel1ReadyText);
        final Button easyLevel1StartBtn = (Button)findViewById(R.id.easyLevel1StartBtn);
        final TextView easyLevel1Rules = (TextView)findViewById(R.id.easyLevel1Rules);
        final CheckBox easyLevel1Check1 = (CheckBox)findViewById(R.id.easyLevel1Check1);
        final CheckBox easyLevel1Check2 = (CheckBox)findViewById(R.id.easyLevel1Check2);
        final CheckBox easyLevel1Check3 = (CheckBox)findViewById(R.id.easyLevel1Check3);
        final CheckBox easyLevel1Check4 = (CheckBox)findViewById(R.id.easyLevel1Check4);
        final Button easyLevel1Next = (Button)findViewById(R.id.easyLevel1Next);
        final Button easyLevel1TryAgain = (Button)findViewById(R.id.easyLevel1TryAgain);
        final MediaPlayer blop = MediaPlayer.create(this, R.raw.blop);
        final MediaPlayer cheer = MediaPlayer.create(this, R.raw.cheer);
        final MediaPlayer drumroll = MediaPlayer.create(this, R.raw.drumroll);
        final MediaPlayer buzzer = MediaPlayer.create(this, R.raw.buzzer);
        final CheckBox[] checks = {easyLevel1Check1, easyLevel1Check2, easyLevel1Check3, easyLevel1Check4};
        myCheck = (r.nextInt(checks.length));

        easyLevel1StartBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(final View view) {
                blop.start();
                easyLevel1Val.setVisibility(View.VISIBLE);
                easyLevel1ReadyText.setVisibility(View.VISIBLE);
                easyLevel1StartBtn.setVisibility(View.INVISIBLE);
                final CountDownTimer put = new CountDownTimer(1000, 1000) {
                    @Override
                    public void onTick(long l) {

                    }

                    @Override
                    public void onFinish() {
                        easyLevel1ReadyText.setText(String.valueOf(ranNumReadyText));
                        checks[myCheck].setText(easyLevel1ReadyText.getText());
                        CountDownTimer take = new CountDownTimer(2000, 2000) {
                            @Override
                            public void onTick(long l) {

                            }
                            @Override

                            public void onFinish() {
                                switch (myCheck) {
                                    case 0:
                                        easyLevel1Check2.setText(setOthers);
                                        easyLevel1Check3.setText(setOthers);
                                        easyLevel1Check4.setText(setOthers);
                                        break;

                                    case 1:
                                        easyLevel1Check1.setText(setOthers);
                                        easyLevel1Check3.setText(setOthers);
                                        easyLevel1Check4.setText(setOthers);
                                        break;

                                    case 2:
                                        easyLevel1Check1.setText(setOthers);
                                        easyLevel1Check2.setText(setOthers);
                                        easyLevel1Check4.setText(setOthers);
                                        break;

                                    case 3:
                                        easyLevel1Check1.setText(setOthers);
                                        easyLevel1Check2.setText(setOthers);
                                        easyLevel1Check3.setText(setOthers);
                                        break;
                                }
                                easyLevel1ReadyText.setText("");
                                easyLevel1ReadyText.setVisibility(View.INVISIBLE);
                                easyLevel1Check1.setVisibility(View.VISIBLE);
                                easyLevel1Check1.setEnabled(true);
                                easyLevel1Check2.setVisibility(View.VISIBLE);
                                easyLevel1Check2.setEnabled(true);
                                easyLevel1Check3.setVisibility(View.VISIBLE);
                                easyLevel1Check3.setEnabled(true);
                                easyLevel1Check4.setVisibility(View.VISIBLE);
                                easyLevel1Check4.setEnabled(true);
                                easyLevel1Rules.setText("When you're done, press validate");
                                time = new CountDownTimer(11000, 1000) {
                                    @Override
                                    public void onTick(long l) {
                                        isRunning = true;
                                        timer--;
                                        easyLevel1Timer.setText("Time Left: " + timer);
                                    }

                                    @Override
                                    public void onFinish() {
                                        isRunning = false;
                                        buzzer.start();
                                        easyLevel1Check1.setEnabled(false);
                                        easyLevel1Check2.setEnabled(false);
                                        easyLevel1Check3.setEnabled(false);
                                        easyLevel1Check4.setEnabled(false);
                                        easyLevel1Next.setEnabled(false);
                                        easyLevel1TryAgain.setEnabled(true);
                                        easyLevel1TryAgain.setBackgroundColor(Color.RED);
                                        easyLevel1TryAgain.setOnClickListener(new View.OnClickListener() {
                                            @Override
                                            public void onClick(View view) {
                                                blop.start();
                                                Intent easyLevel1TryAgain = new Intent(easyLevel1.this, easyLevel1.class);
                                                finish();
                                                startActivity(easyLevel1TryAgain);
                                            }
                                        });
                                    }
                                }.start();
                            }
                        }.start();
                    }
                }.start();
                easyLevel1Next.setVisibility(View.VISIBLE);
                easyLevel1TryAgain.setVisibility(View.VISIBLE);
            }
        });
    }
}

0 个答案:

没有答案