I want to code a number guessing app. It has two activities. One of these is start activity and the we want two inputs (min range and max range) from the user. And then the user hits the start button and goes to the second page. But I cannot go to second page even if I enter inputs for both of the edit texts.
THIS IS MAIN ACTIVITY
class MainActivity : AppCompatActivity() {
private lateinit var binding: ActivityMainBinding
private var minRange: Int? = null
private var maxRange: Int? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
binding = ActivityMainBinding.inflate(layoutInflater)
val view = binding.root
setContentView(view)
minRange = binding.numberMin.text.toString().toIntOrNull()
maxRange = binding.numberMax.text.toString().toIntOrNull()
binding.startBtn.setOnClickListener {
if (minRange != null && maxRange != null) {
val intent: Intent = Intent(this@MainActivity, GameActivity::class.java)
startActivity(intent)
} else {
Toast.makeText(this, "Error", Toast.LENGTH_LONG).show()
}
}
}
}
THIS IS SECOND ACTIVITY
class GameActivity : AppCompatActivity() {
private lateinit var binding : ActivityGameBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_game)
binding = ActivityGameBinding.inflate(layoutInflater)
val view = binding.root
setContentView(view)
var min = intent.getIntExtra("MinRange",0)
var max = intent.getIntExtra("MaxRange",0)
binding.resultText.text = "$min $max"
}
}
I solved the problem . Here is the finished app if you are interested. Keep coding !