每当从另一个活动中获取字符串数据时,程序就会崩溃
我只是制作一个简单的程序,该程序在另一个活动中发送了在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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试用
kanye?.getStringExtra(“ sameer”)?:“ no data”
替换kanye !!。getString(“ Sameer”)
。这样,如果在“ sameer”下未传递任何值和默认的“ no data”
将将程序存储到val shaheer 。如果您在烤面包中看到
没有数据
,则错误地通过了活动之间的变量。Try to replace
kanye!!.getString("sameer")
withkanye?.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 toval shaheer
. If you seeno data
in the toast, you passed the variable between activities incorrectly.您可以在
MainActivity
中执行此操作:您将额外的“ Sameer”放入
Intent
中,该由变量Intent
引用。您可能想将额外的内容放入意图
由变量kash
引用。而是尝试一下:You do this in
MainActivity
:You put the extra with the key "sameer" into the
Intent
referenced by the variableintent
. You probably want to put the extra into theIntent
referenced by the variablekash
. Try this instead: