简体   繁体   English

使用 Epoxy ModelView 会导致 Inflate 错误和 NullPoint 错误

[英]Using the Epoxy ModelView causes Inflate errors and NullPoint errors

I'm using the @ModelView annotation from the Epoxy library to create a CustomView .我正在使用Epoxy库中的@ModelView注释来创建一个CustomView

I'm making it with reference to another example, but the following error keeps coming up.我正在参考另一个示例,但以下错误不断出现。

android.view.InflateException: Binary XML file line #2 in com.example.testepoxy:layout/item_custom_view: Binary XML file line #2 in com.example.testepoxy:layout/my_view: Error inflating class com.example.testepoxy.ItemCustomView
Caused by: java.lang.NullPointerException: findViewById(R.id.title) must not be null
        at com.example.testepoxy.ItemCustomView.<init>(ItemCustomView.kt:21)
        at com.example.testepoxy.ItemCustomView.<init>(ItemCustomView.kt:17)
        at com.example.testepoxy.ItemCustomView.<init>(Unknown Source:11)

Isn't it automatically inflated when using defaultlayout ?使用 defaultlayout 时不会自动膨胀吗? Where did I go wrong?我哪里做错了?

item_custom_view item_custom_view

<?xml version="1.0" encoding="utf-8"?>
<com.example.testepoxy.ItemCustomView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#ff0000">
    <TextView
        android:id="@+id/title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="ewq"/>
</com.example.testepoxy.ItemCustomView>

Model模型

@ModelView(defaultLayout = R.layout.item_custom_view)
class ItemCustomView @JvmOverloads constructor(
    context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : FrameLayout(context, attrs, defStyleAttr) {

    val textView: TextView = findViewById(R.id.title)

    @TextProp
    fun setText(title: CharSequence) {
        textView.text = title
    }
}

Controller控制器

class ItemCustomViewController : TypedEpoxyController<List<String>>() {
    private val TAG = this::class.java.simpleName

    override fun buildModels(data: List<String>?) {
        data?.forEachIndexed { index, s ->
           ItemCustomViewModel_()
               .id(index)
               .text(s)
               .addTo(this)
        }
    }
}

Using lazy may fix your problem.使用lazy可能会解决您的问题。

val textView: TextView by lazy{
     findViewById(R.id.title)
}

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

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