i was working on a demo for kotlin app and separately these activities work but when i tried to link them up with intent ntn is responding to the button that was supposed to send u to the next activity it just doesnt do anyting and on the logcat there is no error showing just the info on where the screen touched so pls i still cant see where the problem lies after trying up all day
package com.example.myapplication
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.provider.AlarmClock.EXTRA_MESSAGE
import android.view.View
import android.widget.Button
import android.widget.Toast
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val tost:Button =findViewById(R.id.toast)
val nxt:Button =findViewById(R.id.next)
tost.setOnClickListener{tst()}
nxt.setOnClickListener{tnxt()}
}
private fun tst(){
Toast.makeText(this,"hello world",Toast.LENGTH_SHORT).show()
}
private fun tnxt(){
Intent(this, diceRoll::class.java)
startActivity(intent)
}
}
//and the diceroll class
package com.example.myapplication
import android.content.Intent
import android.os.Bundle
import android.widget.Button
import android.widget.TextView
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import kotlinx.android.synthetic.main.dice_roll.*
class diceRoll : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.dice_roll)
var bttn:Button =findViewById(R.id.button)
bttn.setOnClickListener {
rolled()
}
}
private fun rolled(){
var txt:TextView=findViewById(R.id.no)
val randomInt=(1..6).random()
val resultStr=randomInt.toString()
txt.setText(resultStr)
}
}
Short answer:
Change your function from
private fun tnxt(){
Intent(this, diceRoll::class.java)
startActivity(intent)
}
to:
private fun tnxt(){
startActivity(Intent(this, diceRoll::class.java))
}
The problem:
With this line Intent(this, diceRoll::class.java)
you're creating an Intent
but never use it.
private fun tnxt(){
Intent(this, diceRoll::class.java)
startActivity(intent)
}
Alternatively,
private fun tnxt(){
val diceRollIntent = Intent(this, diceRoll::class.java) //assigns the intent to a variable which we can use
startActivity(diceRollIntent)
}