如何修复 Failed to invoke private with no args

How to fix Failed to invoke private with no args

我不明白为什么会出现这个错误以及如何修复它。请帮助我

Failed to invoke private ru.osport.osportnfc.model.api.PrintCmd() with no args

我查看了retrofit2中传输数据的结构,没有发现任何错误

@Keep
interface PrintApi {

    @GET
    suspend fun getPrintCommands(@Url url: String, @Query("bib") bib: Int?, @Query("card") card: String): PrintResponse
   //  fun getPrintCommands(@Url url: String, @Query("bib") bib: Int?, @Query("card") card: String): Single<PrintResponse>
}


@Keep
@Serializable
data class PrintResponse(
    val ok: Boolean? = false,
    val error: String = readLine() ?: "", //test
    val printout: List<PrintCmd>? = null
)

@Keep
@Serializable
sealed class PrintCmd()

@Keep
@Serializable
@SerialName("formatted_text")
data class PrintFormatterTextCmd(
    val content: String,
    val left_margin: Float,
    val letter_width: Float,
    val line_height: Float,
    val font_family: String,
    val font_size: Int,
    val align: String,
    val bold: Boolean,
    val italic: Boolean,
    val underline: Boolean
) : PrintCmd()

然后调用本身

@Singleton
class PrintService @Inject constructor(
    private val printManager: PrinterManager,
    @AppScope coroutineScope: CoroutineScope,
    private val api: PrintApi,
    private val eventRepository: EventRepository
) : CoroutineScope by coroutineScope {

    fun print(dao: TagDao, id: Long?, bib: Int?, tagNumber: String = "") = eventRepository.urls.withLatestValue {
        val url = it?.printUrl
        launch {
            try {
                val result = print(url, bib, tagNumber)
                if (id != null) {
                    dao.setPrintStatus(id, if (result) Status.Done else Status.Error)
                }
            }
            catch (e: Exception){
                Log.e("TEST", e.toString())

            }
        }
    }

    suspend fun print(url: String?, bib: Int?, tagNumber: String = ""): Boolean {
        if (url.isNullOrBlank()) {
            return false
        }
        val commands = getCommands(url, bib, tagNumber)
        return commands?.let {
            printManager.printAll(it)
            true
        } ?: false
    }

     private suspend fun getCommands(url: String, bib: Int?, tagNumber: String): List<PrintCmd>? {
    
            try {
                val result = api.getPrintCommands(url, bib, tagNumber)
    
    /**/
                if (result.ok == true && result.error.isBlank()  && result.printout != null ||
                    result.ok == true && result.error == "EVENT_NOT_EDITABLE"  && result.printout != null)
                {
                return result.printout
                }
                else
                {
                    return null
                }
            } catch (e: HttpException) {
                Log.e("TEST", e.message())
                Logger.w(e)
            } catch (e: IOException) {
                Log.e("TEST", e.toString())
                Logger.w(e)
            }
            return null /**/
        } /**/

我尝试更改 PrtintCmd 结构,但没有成功 我什至不明白这个错误可能与什么有关。换库后出现 implementation("com.jakewharton.retrofit:retrofit2-kotlinx-serialization-converter:0.8.0") 到下一个图书馆 implementation "com.squareup.retrofit2:converter-gson:$retrofit"

替换本节代码后

 .addConverterFactory(Json { ignoreUnknownKeys = true }.asConverterFactory("application/json".toMediaType()))

到本节以下代码

.addConverterFactory(GsonConverterFactory.create())

请帮我想想办法

堆栈跟踪:

W/System.err: java.lang.RuntimeException: Failed to invoke private ru.osport.osportnfc.model.api.PrintCmd() with no args W/System.err:
at com.google.gson.internal.ConstructorConstructor.construct(ConstructorConstructor.java:113) W/System.err: at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:212) W/System.err: at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.read(TypeAdapterRuntimeTypeWrapper.java:41) W/System.err: at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:82) W/System.err: at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:61) W/System.err: at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.read(ReflectiveTypeAdapterFactory.java:131) W/System.err: at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:222) W/System.err: at retrofit2.converter.gson.GsonResponseBodyConverter.convert(GsonResponseBodyConverter.java:40) W/System.err: at retrofit2.converter.gson.GsonResponseBodyConverter.convert(GsonResponseBodyConverter.java:27) W/System.err: at retrofit2.OkHttpCall.parseResponse(OkHttpCall.java:243) W/System.err: at retrofit2.OkHttpCall.onResponse(OkHttpCall.java:153) W/System.err: at okhttp3.internal.connection.RealCall$AsyncCall.run(RealCall.kt:519) W/System.err: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167) W/System.err: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641) W/System.err: at java.lang.Thread.run(Thread.java:764) W/System.err: Caused by: java.lang.InstantiationException: Can't instantiate abstract class ru.osport.osportnfc.model.api.PrintCmd W/System.err: at java.lang.reflect.Constructor.newInstance0(Native Method) W/System.err: at java.lang.reflect.Constructor.newInstance(Constructor.java:343) W/System.err: at com.google.gson.internal.ConstructorConstructor.construct(ConstructorConstructor.java:110) W/System.err: ... 14 more

Hej 以太网,

我认为错误是您正在尝试实例化一个密封的 class:

java.lang.InstantiationException: Can't instantiate abstract class ru.osport.osportnfc.model.api.PrintCmd W/System.err: at 

在您的 PrintResponse 中,您需要一个 PrintCmd 列表,因此转换器会尝试解析对此 class 的响应,但无法实例化 sealed class。我认为您必须将类型更改为实现:

data class PrintResponse(
    val ok: Boolean? = false,
    val error: String = readLine() ?: "", //test
    val printout: List<PrintFormatterTextCmd>? = null
)