【问题标题】:intent on kotlin on android studio not working打算在 android studio 上使用 kotlin 不起作用
【发布时间】:2023-03-15 03:51:01
【问题描述】:

我正在为 kotlin 应用程序进行演示,并且这些活动单独工作,但是当我尝试将它们与意图链接起来时,ntn 正在响应应该将你发送到下一个活动的按钮,它只是不做任何事情并且继续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)
    }

    
}

//和骰子类

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)


    }
}

【问题讨论】:

    标签: java android kotlin android-intent


    【解决方案1】:

    简答:

    改变你的功能

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-27
      • 2021-08-02
      • 2019-08-10
      • 2021-07-12
      • 1970-01-01
      • 2021-11-17
      相关资源
      最近更新 更多