I'm using the @ModelView
annotation from the Epoxy
library to create a 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 ? Where did I go wrong?
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)
}
}
}
CodePudding user response:
Using lazy
may fix your problem.
val textView: TextView by lazy{
findViewById(R.id.title)
}