简体   繁体   English

Kotlin TypeToken“未解决的参考”错误

[英]Kotlin TypeToken “Unresolved reference” error

I am getting error "Unresolved reference to response" in line number 5,6 and 7. Can you please help me understand the problem? 我在第5,6和7行收到错误“未解决的回复参考”。你能帮助我理解这个问题吗?

 public class GenericCall {
        public fun genecicCall(functionToBeCalled:String, responseType:String, requestType:String, vararg args:String){
            var request = Class.forName(requestType).newInstance()
            var response = Class.forName(responseType).newInstance()
            var finalType = object : TypeToken<GenericResponse<Class<response>>>(){}.getType()
            var creditresponse: GenericResponse<response>? = AjaxHelper.ajax(Constants.Ajax.ENDPOINT_CREDIT, Constants.Ajax.REQUEST_POST, request, finalType, null)
            return ResponseProcessing.processResponse(creditresponse as Any) as response?
        }
        }
    }

Generic type argument can't be an expression, it should be another type, such as a class, possibly with other generic type arguments. 泛型类型参数不能是表达式,它应该是另一种类型,例如类,可能还有其他泛型类型参数。 So instead of Class<response> (line #5) you should write for example Class<Foo> if Foo is the static (inferred by the compiler) type of response . 因此,如果Foo是静态(由编译器推断)类型的response则应该编写例如Class<Foo>而不是Class<response> (第5行)。 In your case you probably don't know anything about response apart from the fact that it's an instance of Any . 在你的情况下,你可能对response一无所知,除了它是Any的一个实例。 So you can use Class<Any> or Class<*> , whichever you prefer. 因此,您可以使用Class<Any>Class<*> ,无论您喜欢哪个。 Similarly with GenericResponse (line #6). GenericResponse类似(第6行)。

Also expression cannot appear in the right-hand side of the as operator for the same reason (line #7): this operator casts the left-hand side to the type specified in the right-hand side. 由于相同的原因(第7行),表达式也不能出现在as运算符的右侧:此运算符将左侧转换为右侧指定的类型 Additionally, your function doesn't seem to return anything, so returning a useful value from it won't work. 此外,您的函数似乎没有返回任何内容,因此从中返回有用的值将不起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM