代码之家  ›  专栏  ›  技术社区  ›  Chris Edgington

保存FireStore对象会引发InvocationTargetException错误

  •  1
  • Chris Edgington  · 技术社区  · 6 年前

    我花了大约一个小时调试这个问题,所以我想知道是否有人能够解释这里发生的事情。

    我正在尝试在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() 你也会得到同样的错误。

    1 回复  |  直到 6 年前
        1
  •  1
  •   Nick Skudnyakov    6 年前

    CustomClassMapper

    private static boolean shouldIncludeGetter(Method method) {
        if (!method.getName().startsWith("get") && !method.getName().startsWith("is")) {
            return false;
        } else if (method.getDeclaringClass().equals(Object.class)) {
            return false;
        } else if (!Modifier.isPublic(method.getModifiers())) {
            return false;
        } else if (Modifier.isStatic(method.getModifiers())) {
            return false;
        } else if (method.getReturnType().equals(Void.TYPE)) {
            return false;
        } else if (method.getParameterTypes().length != 0) {
            return false;
        } else {
            return !method.isAnnotationPresent(Exclude.class);
        }
    }
    

    InvocationTargetException