[英]Using the Epoxy ModelView causes Inflate errors and NullPoint errors
我正在使用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)
}
}
}
使用lazy
可能会解决您的问题。
val textView: TextView by lazy{
findViewById(R.id.title)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.