Answer the question
In order to leave comments, you need to log in
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();
}
//Обрабатываем нажатие кнопки конец
}
});
Answer the question
In order to leave comments, you need to log in
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 questionAsk a Question
731 491 924 answers to any question