0

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)
        }
    }
}
ybybyb
  • 1,385
  • 1
  • 12
  • 33

2 Answers2

1

Using lazy may fix your problem.

val textView: TextView by lazy{
     findViewById(R.id.title)
}
Farid
  • 1,024
  • 9
  • 16
  • thank you i solved it But why did the original code get an error? When `ItemCustomViewModel_()` is executed, `findViewById` of Model class is not executed first, why is `setText` executed first? – ybybyb Jul 06 '22 at 17:40
  • 1
    If you check error again you can see that error is raised when it tries to 'findViewById' and not in 'setText'. – Farid Jul 07 '22 at 18:30
0

You can do this way to take advantage of ViewBinding:

R.layout.item_custom_view:

<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <androidx.appcompat.widget.AppCompatTextView
        android:id="@+id/title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
</merge>
@ModelView(autoLayout = ModelView.Size.MATCH_WIDTH_WRAP_HEIGHT)
class ItemCustomView @JvmOverloads constructor(
    context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : FrameLayout(context, attrs, defStyleAttr) {
    private val binding = ItemCustomViewBinding.inflate(LayoutInflater.from(context), this)

    // if you need to expose your child view
    val textView: TextView get()= binding.title

    @TextProp
    fun setText(title: CharSequence) {
        // or
        // binding.title.text = title
        textView.text = title
    }}
beigirad
  • 4,986
  • 2
  • 29
  • 52