Приложение вылетает при нажатии кнопки при переключении на фрагмент и обратно ⇐ Android
Приложение вылетает при нажатии кнопки при переключении на фрагмент и обратно
Why is it that when I go from the home slice to another slice and then back to the home slice and when I click on the hitButton, the application crashes out
class MainActivity : AppCompatActivity() { private lateinit var binding: ActivityMainBinding private var score = 0 private lateinit var scoreTextView: TextView private var pelmen:Int = 0
override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) binding = ActivityMainBinding.inflate(layoutInflater) setContentView(binding.root) val navView: BottomNavigationView = binding.navView val navController = findNavController(R.id.nav_host_fragment_activity_main) val appBarConfiguration = AppBarConfiguration( setOf( R.id.navigation_home, R.id.navigation_dashboard, R.id.navigation_notifications ) ) setupActionBarWithNavController(navController, appBarConfiguration) navView.setupWithNavController(navController) scoreTextView = findViewById(R.id.scoreTextView) if(savedInstanceState != null) { score = savedInstanceState.getInt("score") scoreTextView.text = "счёт: $score" } val hitButton: Button = findViewById(R.id.hitButton) hitButton.setOnClickListener { score++ scoreTextView.text = "счёт: $score" val mediaPlayer = MediaPlayer.create(applicationContext, R.raw.punch) mediaPlayer.start() val imageView: ImageView = findViewById(R.id.sergayView) imageView.setImageResource(R.drawable.malyshanim) if (score == 50) { scoreTextView.text = "вы получили новый уровень!" } Handler().postDelayed({ imageView.setImageResource(R.drawable.malysh) }, 200) } } override fun onSaveInstanceState(outState: Bundle) { super.onSaveInstanceState(outState) outState.putInt("score", score) } override fun onRestoreInstanceState(savedInstanceState: Bundle) { super.onRestoreInstanceState(savedInstanceState) score = savedInstanceState.getInt("score") scoreTextView.text = "счёт: $score" } }
I would like to know how to solve this problem
Источник: https://stackoverflow.com/questions/781 ... ent-and-ba
Why is it that when I go from the home slice to another slice and then back to the home slice and when I click on the hitButton, the application crashes out
class MainActivity : AppCompatActivity() { private lateinit var binding: ActivityMainBinding private var score = 0 private lateinit var scoreTextView: TextView private var pelmen:Int = 0
override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) binding = ActivityMainBinding.inflate(layoutInflater) setContentView(binding.root) val navView: BottomNavigationView = binding.navView val navController = findNavController(R.id.nav_host_fragment_activity_main) val appBarConfiguration = AppBarConfiguration( setOf( R.id.navigation_home, R.id.navigation_dashboard, R.id.navigation_notifications ) ) setupActionBarWithNavController(navController, appBarConfiguration) navView.setupWithNavController(navController) scoreTextView = findViewById(R.id.scoreTextView) if(savedInstanceState != null) { score = savedInstanceState.getInt("score") scoreTextView.text = "счёт: $score" } val hitButton: Button = findViewById(R.id.hitButton) hitButton.setOnClickListener { score++ scoreTextView.text = "счёт: $score" val mediaPlayer = MediaPlayer.create(applicationContext, R.raw.punch) mediaPlayer.start() val imageView: ImageView = findViewById(R.id.sergayView) imageView.setImageResource(R.drawable.malyshanim) if (score == 50) { scoreTextView.text = "вы получили новый уровень!" } Handler().postDelayed({ imageView.setImageResource(R.drawable.malysh) }, 200) } } override fun onSaveInstanceState(outState: Bundle) { super.onSaveInstanceState(outState) outState.putInt("score", score) } override fun onRestoreInstanceState(savedInstanceState: Bundle) { super.onRestoreInstanceState(savedInstanceState) score = savedInstanceState.getInt("score") scoreTextView.text = "счёт: $score" } }
I would like to know how to solve this problem
Источник: https://stackoverflow.com/questions/781 ... ent-and-ba
-
- Похожие темы
- Ответы
- Просмотры
- Последнее сообщение