【问题标题】:@RawValue annotation is not applicable to target value parameter@RawValue 注解不适用于目标值参数
【发布时间】:2018-04-02 06:09:02
【问题描述】:

我正在尝试Parcelize 一个数据类。 它包含一个参数:

var tokenType: Any? = null

对于这个变量编译器在编译时抱怨:

Type is not directly supported by Parcelize. 
Annotate the parameter with @RawValue if you want it to be serialized via 
writeValue()

虽然错误是不言自明的,但当我像这样添加@RawValue 时:

@RawValue var tokenType: Any? = null

它给出了一个错误:

This annotation is not applicable to the target value parameter

关于如何处理这个问题的任何提示?

【问题讨论】:

    标签: android kotlin


    【解决方案1】:

    我从Kotlang 社区得到了这个问题的答案。 答案是你不能注释变量本身 但是你必须注释它的类型。

    所以用以下方式注释可以消除错误:

     var tokenType: @RawValue Any? = null
    

    尽管不要忘记手动为此属性编写序列化器/反序列化器,因为它不会自动完成。

    希望对你有帮助。

    【讨论】:

    • 看到这个答案我顿时明白了目标注解是什么意思,谢谢!
    • Any 类型的序列化器/反序列化器怎么写?
    • @Beast77 请使用普通的 writeToParcel 和 readFromParcel 方法,与不使用 Parcelize 关键字的方法相同。
    • @Tarun 那你为什么要使用 Parcelize?
    • 仍在工作...非常感谢
    猜你喜欢
    • 1970-01-01
    • 2017-03-15
    • 1970-01-01
    • 2021-06-10
    • 2021-02-27
    • 1970-01-01
    • 1970-01-01
    • 2015-12-12
    • 2014-08-12
    相关资源
    最近更新 更多