【发布时间】:2021-01-11 19:59:28
【问题描述】:
android studio 4.1.1 和 Kotlin。我是通过教程学习的初学者。
我有一个活动,我正在使用 Intent 去另一个活动。我正在使用intent.putExtra:
val intent = Intent(this, WeightExercise::class.java)
intent.putExtra("POSITION", positionToString)
startActivity(intent)
在 WeightExercise 活动中,我得到了传递的值:
// with line numbers:
80: var myPosition = intent.getStringExtra("POSITION").toString()
81: var position = myPosition.toInt()
这行得通。我在POSITION 中得到字符串值。
问题:我有另一个使用 Intent 进入 WeightExercise 活动的活动。它不是通过意图传递值。发生这种情况时,WeightExercise 活动中的第 81 行(上方)会导致错误:
Caused by: java.lang.NumberFormatException: For input string: "null"
at java.lang.Integer.parseInt(Integer.java:615)
at java.lang.Integer.parseInt(Integer.java:650)
at com.johndcowan.liftit.WeightExercise.onCreate(WeightExercise.kt:81)
我已经尝试了几个 if()s 来测试 null 或空值,但没有任何东西能解决这个错误。
if ( intent.getStringExtra("POSITION").toString().isNullOrEmpty() ) {...}
if ( myPosition.isNullOrEmpty() ) {...}
if ( myPosition == null ) {...}
有没有更好的方法来测试意图中的命名字符串POSITION?
感谢任何提示。
【问题讨论】:
标签: android-studio kotlin android-intent