【问题标题】:Using the Epoxy ModelView causes Inflate errors and NullPoint errors使用 Epoxy ModelView 会导致 Inflate 错误和 NullPoint 错误
【发布时间】:2022-07-05 20:22:44
【问题描述】:

我正在使用Epoxy 库中的@ModelView 注释来创建CustomView

我正在参考另一个示例,但以下错误不断出现。

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)

使用 defaultlayout 时不会自动膨胀吗?我哪里做错了?

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>

型号

@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
    }
}

控制器

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)
        }
    }
}

【问题讨论】:

    标签: android kotlin epoxy


    【解决方案1】:

    使用lazy 可能会解决您的问题。

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

    【讨论】:

      猜你喜欢
      • 2018-08-08
      • 2017-12-22
      • 1970-01-01
      • 2018-11-12
      • 1970-01-01
      • 1970-01-01
      • 2019-03-26
      • 1970-01-01
      • 2014-12-04
      相关资源
      最近更新 更多