V
V
Vladislav Khrushchev2021-02-13 17:52:34
Java
Vladislav Khrushchev, 2021-02-13 17:52:34

The game crashes after writing the code for the back button. How to fix?

In general, I'm making a simple game for android. I came across a problem in the face of a game crash on the emulator after entering the first level. She started to fly out after writing the code for the "back" button (not the system one). The funny thing is that neither android studio nor the emulator finds any errors. Here is a piece of code that crashes the game

Button btn_back = (Button)findViewById(R.id.button_back);
        btn_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //Обрабатываем нажатие кнопки начало
                try {
                    //Вернуться назад к выбору уровня - начало
                    Intent intent = new Intent (Level1.this, GameLevels.class); //создали намерения для перехода
                    startActivity(intent); finish();
                    //Вернуться назад к выбору уровня - конец
                } catch (Exception e) {
                    e.printStackTrace();
                }
                //Обрабатываем нажатие кнопки конец
            }
        });

and this is what the emulator writes after the game crashes
6027e76e49df6751401022.png

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Vodakov, 2021-02-14
@WaterSmith

Since the code inside the listner is all in your attempt, it cannot crash. This means that the very appeal to the btn_back variable through a dot is crashing. It can be if btn_back = null
If findViewById returns null to you, then the view with id button_back is not in the layout you are working with.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question