Search code examples
javaandroidkotlinrx-android

Generic class input param type miss match


Always get an Error type mismatch, when input param for getObjectSingle. how do I fix with my generic wrapper class?

enter image description here

enter image description here


Solution

  • You need the class not the instance, but you must create an instance of the class to get class itself, try this:

    val wrapper = Wrapper<String>()
    
    Rx2AndroidNetworking.post(someUrl)
            .build()
            .getObjectSingle(wrapper.javaClass)
    

    OR

    val wrapper = Wrapper<String>()
    
    Rx2AndroidNetworking.post(someUrl)
            .build()
            .getObjectSingle(wrapper::class.java)
    

    OR

    val wrapper = Wrapper<String>()
    
    val s: Class<Wrapper<String>> = wrapper.javaClass
    
    Rx2AndroidNetworking.post(someUrl)
            .build()
            .getObjectSingle(s)