每当从另一个活动中获取字符串数据时,程序就会崩溃

发布于 2025-02-04 19:50:42 字数 1133 浏览 2 评论 0原文

我只是制作一个简单的程序,该程序在另一个活动中发送了在EditText中输入的字符串,我是初学者,并且在Internet上关注Kotlin教程,每当该程序从第一个活动获取字符串时,该程序就会崩溃,第一活动的代码看起来像这样:

class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)
    findViewById<Button>(R.id.button).setOnClickListener {

        val message: String? = findViewById<EditText>(R.id.sameer).text.toString()
        val kash = Intent(this, kos::class.java)
        intent.putExtra("sameer", message)
        startActivity(kash)



    }
}
}

第二个活动的代码是这样的:

class kos: AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {

    super.onCreate(savedInstanceState)
    setContentView(R.layout.kos)
    val kanye: Bundle? = intent.extras
    val shaheer = kanye!!.getString("sameer")
    Toast.makeText(this, shaheer , Toast.LENGTH_SHORT)
    findViewById<TextView>(R.id.UserisHambola).text = shaheer

}




}

我尝试检查错误何时开始发生,看来它在声明变量“ shaheer”之后开始发生第二个活动,如果我在其他所有代码之后删除该代码,则该程序不会崩溃,我不知道为什么会发生崩溃,谢谢您的时间; 3

I was just making a simple program that sends string that's typed in an EditText into a TextView in another activity, I'm a beginner and was following a kotlin tutorial on the internet, the program would crash whenever it gets the string from the 1st activity, The code of the 1st activity looks like this:

class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)
    findViewById<Button>(R.id.button).setOnClickListener {

        val message: String? = findViewById<EditText>(R.id.sameer).text.toString()
        val kash = Intent(this, kos::class.java)
        intent.putExtra("sameer", message)
        startActivity(kash)



    }
}
}

and the code for the 2nd activity goes like this:

class kos: AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {

    super.onCreate(savedInstanceState)
    setContentView(R.layout.kos)
    val kanye: Bundle? = intent.extras
    val shaheer = kanye!!.getString("sameer")
    Toast.makeText(this, shaheer , Toast.LENGTH_SHORT)
    findViewById<TextView>(R.id.UserisHambola).text = shaheer

}




}

I've tried to check when does the error start to happen, it appears that it starts to happen after declaring variable "shaheer" in the 2nd activity, if I delete that code with all the other codes after it, the program doesn't crash, I don't know why does this crash happen, Thank you for your time <3

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

|煩躁 2025-02-11 19:50:43

尝试用kanye?.getStringExtra(“ sameer”)?:“ no data”替换kanye !!。getString(“ Sameer”)。这样,如果在“ sameer”下未传递任何值和默认的“ no data”将将程序存储到val shaheer 。如果您在烤面包中看到没有数据,则错误地通过了活动之间的变量。

Try to replace kanye!!.getString("sameer") with kanye?.getStringExtra("sameer") ?: "no data". This way the program will not crash if no value is passed under "sameer" key and the default "no data" will be stored to val shaheer. If you see no data in the toast, you passed the variable between activities incorrectly.

苍白女子 2025-02-11 19:50:43

您可以在MainActivity中执行此操作:

    val kash = Intent(this, kos::class.java)
    intent.putExtra("sameer", message)
    startActivity(kash)

您将额外的“ Sameer”放入Intent中,该由变量Intent引用。您可能想将额外的内容放入意图由变量kash引用。而是尝试一下:

    val kash = Intent(this, kos::class.java)
    kash.putExtra("sameer", message)
    startActivity(kash)

You do this in MainActivity:

    val kash = Intent(this, kos::class.java)
    intent.putExtra("sameer", message)
    startActivity(kash)

You put the extra with the key "sameer" into the Intent referenced by the variable intent. You probably want to put the extra into the Intent referenced by the variable kash. Try this instead:

    val kash = Intent(this, kos::class.java)
    kash.putExtra("sameer", message)
    startActivity(kash)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文