【问题标题】:Android Kotlin Parcelize android extension giving errorAndroid Kotlin Parcelize android 扩展给出错误
【发布时间】:2020-03-03 07:23:23
【问题描述】:

我已使用 Android Extension 的 Parcelize 属性在 Kotlin 中获取 Parcelable 数据类。它大大减少了样板代码。最初它处于实验模式,但现在稳定。 我已经按照下面链接的教程 Parcelable Link 我在 gradle 中进行了更改以添加

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-android'



androidExtensions {
    features = ["parcelize"]
}

最初,它没有给出任何编译器错误,但在运行应用程序时,它给出了未解决的参考错误,因为 viewbinding did with layout from kotlin。 导入很好,如果不包含Parcelize 属性,它可以正常工作。

import kotlinx.android.synthetic.main.activity.*

可能是什么问题? 它是否只告诉使用 android 扩展的 Parcelize 属性并保留 android-extension 插件中包含的其他属性?

【问题讨论】:

    标签: android android-studio kotlin runtime-error parcelable


    【解决方案1】:
    1. 您不需要在 gradle 文件中进行更改,例如:

      androidExtensions {
      features = ["parcelize"]
      

    只需确保您的库是最新的。

    1. 确保您正在使用annotation 并实施Parcelable

    @Parcelize class YourClass : Parcelable

    而且导入看起来像这样:

    import kotlinx.android.parcel.Parcelize

    【讨论】:

    • 实际上,Parcelize 没有问题,它导致 Activity 类和布局视图绑定之间的 ViewBinding 出现问题......我直接从视图布局中使用的 id 调用活动中的视图,如 kotlin .. . 它为活动中使用的 id 提供未解决的引用
    • 使用 binding.yourView 而不是 R.id.yourView
    • 目前,我可以直接在我的 .kt 文件中使用它,如下所示。 first_button1.setOnClickListener(this) second_button2.setOnClickListener(this) 其中 first_button1 和 second_button2 是 Button 布局中的 id
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-10
    • 1970-01-01
    • 1970-01-01
    • 2018-07-03
    • 1970-01-01
    相关资源
    最近更新 更多