Exception kotlin使用bundle.getString尝试捕获块
我们有传递回活动的意向附加内容Exception kotlin使用bundle.getString尝试捕获块,exception,kotlin,try-catch,Exception,Kotlin,Try Catch,我们有传递回活动的意向附加内容 代码是用Kotlin 1.3编写的,发布在下面 我们不理解为什么代码需要在try-catch块中 我们的问题是,是否有更好的方法来编写此代码,是否有人可以解释为什么代码需要try-catch块。我们知道它可以在什么时候写。 返回此活动的导航是通过各种意图完成的,这些意图并不总是放置捆绑包获得的所有值。 一个按钮使用此代码 val intent = Intent(this,MainActivity::class.java) intent.putExt
代码是用Kotlin 1.3编写的,发布在下面
我们不理解为什么代码需要在try-catch块中
我们的问题是,是否有更好的方法来编写此代码,是否有人可以解释为什么代码需要try-catch块。我们知道它可以在什么时候写。
返回此活动的导航是通过各种意图完成的,这些意图并不总是放置捆绑包获得的所有值。
一个按钮使用此代码
val intent = Intent(this,MainActivity::class.java)
intent.putExtra("FROM", "NEW")
intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION)
startActivity(intent)
holder.ivEdit.setOnClickListener {
//val rowid = friendList.get(position).id
val intent = Intent(context, MainActivity::class.java)
intent.putExtra("FROM", "UPDATE")
intent.putExtra("recordID", items.id)
intent.putExtra("PERSON", items.person)
intent.putExtra("PHONE", items.phone)
intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
context.startActivity(intent)
}
而另一个按钮使用此代码 val intent = Intent(this,MainActivity::class.java)
intent.putExtra("FROM", "NEW")
intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION)
startActivity(intent)
holder.ivEdit.setOnClickListener {
//val rowid = friendList.get(position).id
val intent = Intent(context, MainActivity::class.java)
intent.putExtra("FROM", "UPDATE")
intent.putExtra("recordID", items.id)
intent.putExtra("PERSON", items.person)
intent.putExtra("PHONE", items.phone)
intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
context.startActivity(intent)
}
以下是包含try-catch代码的活动中的代码代码在onCreate函数中
try {
val bundle: Bundle = intent.extras
from = bundle.getString("FROM","")
txtPerson = bundle.getString("PERSON","")
txtPhone = bundle.getString("PHONE","")
if(from == "UPDATE") {
showMSG("To CANCEL use back button")
id = bundle.getInt("recordID", 4)
btnAdd.visibility = View.INVISIBLE
btnEdit.visibility = View.VISIBLE
btnViewList.visibility = View.INVISIBLE
etPerson.setText(txtPerson)
etPhone.setText(txtPhone)
}else if (from == "DELETE"){
showMSG("To CANCEL use back button")
btnAdd.visibility = View.INVISIBLE
btnViewList.visibility = View.INVISIBLE
btnEdit.visibility = View.INVISIBLE
btnDelete.visibility = View.VISIBLE
etPerson.setText(txtPerson)
etPhone.setText(txtPhone)
etPerson.isEnabled = false
etPhone.isEnabled = false
}else{
btnViewList.visibility = View.VISIBLE
btnAdd.visibility = View.VISIBLE
btnEdit.visibility = View.INVISIBLE
}
if (id != 0) {
//etPerson.setText(txtPerson)
//etPhone.setText(txtPhone)
}
} catch (ex: Exception) {
}
这里的猜测是,带有try-catch的活动也被另一个没有传递捆绑包信息的活动导航到,因此捆绑包被设置为null
intent.extras不能为null因此,如果为null,则需要一种处理该事实的方法
我看不到比try-catch块更好的解决方法
也许有人可以提供另一种解决方案。