Почему активность не открывается - PullRequest
0 голосов
/ 05 апреля 2020

Я создал проект в Android Studio с двумя действиями. В первом упражнении у меня есть кнопка, которая открывает второе занятие, но когда я нажимаю на нее, второе занятие вылетает. Тем не менее, если я запишу java только второго действия в новый проект, он будет работать без ошибок. Может кто-нибудь сказать мне, в чем проблема?

Java для первого действия:

public class MainActivity extends AppCompatActivity {

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

    Button btn = (Button) findViewById(R.id.button_start_quiz);
    btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            openQuizActivity();
        }
    });
}

public void openQuizActivity() {
    Intent intent = new Intent(MainActivity.this, QuizActivity.class);
    startActivity(intent);
}
}

Java для второго действия:

public class QuizActivity extends AppCompatActivity {

Button answer1, answer2, answer3, answer4;

ImageView flag;

List<CountryItem> list;

Random r;

int turn = 1;

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

    answer1 = (Button) findViewById(R.id.answer_1);
    answer2 = (Button) findViewById(R.id.answer_2);
    answer3 = (Button) findViewById(R.id.answer_3);
    answer4 = (Button) findViewById(R.id.answer_4);

    flag = (ImageView) findViewById(R.id.flag);

    list = new ArrayList<>();

    r = new Random();

    for (int i = 0; i < new Database().answers.length; i++) {
        list.add(new CountryItem(new Database().answers[i], new Database().flags[i]));
    }

    Collections.shuffle(list);

    newQuestion(turn);

    answer1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (answer1.getText().toString().equalsIgnoreCase(list.get(turn - 1).getName())) {
                Toast.makeText(QuizActivity.this, "Corect !", Toast.LENGTH_SHORT).show();
                if(turn < list.size()) {
                    turn++;
                    newQuestion(turn);
                } else {
                    Toast.makeText(QuizActivity.this, "you finished the game", Toast.LENGTH_SHORT).show();
                }
            } else {
                Toast.makeText(QuizActivity.this, "wrong !", Toast.LENGTH_SHORT).show();
                Toast.makeText(QuizActivity.this, "you lost the game !", Toast.LENGTH_SHORT).show();
            }
        }
    });

    answer2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (answer2.getText().toString().equalsIgnoreCase(list.get(turn - 1).getName())) {
                Toast.makeText(QuizActivity.this, "Corect !", Toast.LENGTH_SHORT).show();
                if(turn < list.size()) {
                    turn++;
                    newQuestion(turn);
                } else {
                    Toast.makeText(QuizActivity.this, "you finished the game", Toast.LENGTH_SHORT).show();
                }
            } else {
                Toast.makeText(QuizActivity.this, "wrong !", Toast.LENGTH_SHORT).show();
                Toast.makeText(QuizActivity.this, "you lost the game !", Toast.LENGTH_SHORT).show();
            }
        }
    });

    answer3.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (answer3.getText().toString().equalsIgnoreCase(list.get(turn - 1).getName())) {
                Toast.makeText(QuizActivity.this, "Corect !", Toast.LENGTH_SHORT).show();
                if(turn < list.size()) {
                    turn++;
                    newQuestion(turn);
                } else {
                    Toast.makeText(QuizActivity.this, "you finished the game", Toast.LENGTH_SHORT).show();
                }
            } else {
                Toast.makeText(QuizActivity.this, "wrong !", Toast.LENGTH_SHORT).show();
                Toast.makeText(QuizActivity.this, "you lost the game !", Toast.LENGTH_SHORT).show();
            }
        }
    });

    answer4.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (answer4.getText().toString().equalsIgnoreCase(list.get(turn - 1).getName())) {
                Toast.makeText(QuizActivity.this, "Corect !", Toast.LENGTH_SHORT).show();
                if(turn < list.size()) {
                    turn++;
                    newQuestion(turn);
                } else {
                    Toast.makeText(QuizActivity.this, "you finished the game", Toast.LENGTH_SHORT).show();
                }
            } else {
                Toast.makeText(QuizActivity.this, "wrong !", Toast.LENGTH_SHORT).show();
                Toast.makeText(QuizActivity.this, "you lost the game !", Toast.LENGTH_SHORT).show();
            }
        }
    });
}

private void newQuestion(int number) {
    flag.setImageResource(list.get(number - 1).getImage());

    int correct_answer = r.nextInt(4) + 1;

    int firstButton = number - 1;
    int secondButton;
    int thirdButton;
    int forthButton;

    switch (correct_answer) {
        case 1:
            answer1.setText(list.get(firstButton).getName());

            do {
                secondButton = r.nextInt(list.size());
            } while (secondButton == firstButton);
            do {
                thirdButton = r.nextInt(list.size());
            } while (thirdButton == firstButton || thirdButton == secondButton);
            do {
                forthButton = r.nextInt(list.size());
            } while (forthButton == firstButton || forthButton == secondButton || forthButton == thirdButton);

            answer2.setText(list.get(secondButton).getName());
            answer3.setText(list.get(thirdButton).getName());
            answer4.setText(list.get(forthButton).getName());

            break;
        case 2:
            answer2.setText(list.get(firstButton).getName());

            do {
                secondButton = r.nextInt(list.size());
            } while (secondButton == firstButton);
            do {
                thirdButton = r.nextInt(list.size());
            } while (thirdButton == firstButton || thirdButton == secondButton);
            do {
                forthButton = r.nextInt(list.size());
            } while (forthButton == firstButton || forthButton == secondButton || forthButton == thirdButton);

            answer1.setText(list.get(secondButton).getName());
            answer3.setText(list.get(thirdButton).getName());
            answer4.setText(list.get(forthButton).getName());

            break;
        case 3:
            answer3.setText(list.get(firstButton).getName());

            do {
                secondButton = r.nextInt(list.size());
            } while (secondButton == firstButton);
            do {
                thirdButton = r.nextInt(list.size());
            } while (thirdButton == firstButton || thirdButton == secondButton);
            do {
                forthButton = r.nextInt(list.size());
            } while (forthButton == firstButton || forthButton == secondButton || forthButton == thirdButton);

            answer2.setText(list.get(secondButton).getName());
            answer1.setText(list.get(thirdButton).getName());
            answer4.setText(list.get(forthButton).getName());

            break;
        case 4:
            answer4.setText(list.get(firstButton).getName());

            do {
                secondButton = r.nextInt(list.size());
            } while (secondButton == firstButton);
            do {
                thirdButton = r.nextInt(list.size());
            } while (thirdButton == firstButton || thirdButton == secondButton);
            do {
                forthButton = r.nextInt(list.size());
            } while (forthButton == firstButton || forthButton == secondButton || forthButton == thirdButton);

            answer2.setText(list.get(secondButton).getName());
            answer3.setText(list.get(thirdButton).getName());
            answer1.setText(list.get(forthButton).getName());

            break;
    }
}
}
...