我花了大约一个小时调试这个问题,所以我想知道是否有人能够解释这里发生的事情。
我正在尝试在Android(在Kotlin)上保存FireStore自定义对象。这是一个简化版-
class MockTransaction {
val endpoint: String = "mockTransactions"
var fromAddress: String = ""
var toAddress: String = ""
var amount: Double = 0.0
constructor() {}
constructor(from: String, to: String, amount: Double) {
this.fromAddress = from
this.toAddress = to
this.amount = amount
}
fun isValid(): Boolean {
return true
}
fun save() {
val db = FirebaseFirestore.getInstance()
db.collection(endpoint).add(this).addOnCompleteListener {
Log.d("TX", it.isSuccessful.toString() )
Log.d("TX", it.exception.toString())
}
}
}
val tx = MockTransaction("Alice", "Bob", 100.0)
tx.save()
现在如果我改变
isValid
函数引发这样的异常
fun isValid(): Boolean {
throw TransactionException("No signature found")
}
带有引发错误的保存方法
java.lang.reflect.invocationTargetException(调用目标异常)
如果我改变功能
名称
简单地
fun valid()
然后保存就开始了。
我只是好奇为什么函数名很重要?我也试着把它改成
fun isARadioactiveSpider()
你也会得到同样的错误。