android studio 上的 kotlin 不工作

intent on kotlin on android studio not working

我正在为 kotlin 应用程序开发一个演示,并且这些活动单独工作,但是当我尝试 link 它们时,ntn 正在响应应该将你发送到下一个的按钮 activity 它只是没有做任何事情,在 logcat 上没有错误只显示屏幕触摸位置的信息所以请问我在尝试了一整天后仍然看不出问题出在哪里

    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)
    }

    
}

//和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)


    }
}

简答:

改变你的功能

private fun tnxt(){
      Intent(this, diceRoll::class.java)
    startActivity(intent)
}

至:

private fun tnxt(){
    startActivity(Intent(this, diceRoll::class.java))
}

问题:

使用此行 Intent(this, diceRoll::class.java) 您正在创建一个 Intent 但永远不要使用它。

private fun tnxt(){
    Intent(this, diceRoll::class.java)
    startActivity(intent)
}

或者,

private fun tnxt(){
    val diceRollIntent = Intent(this, diceRoll::class.java) //assigns the intent to a variable which we can use
    startActivity(diceRollIntent)
}