首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用Epoxy ModelView会导致膨胀错误和NullPoint错误

使用Epoxy ModelView会导致膨胀错误和NullPoint错误
EN

Stack Overflow用户
提问于 2022-07-05 08:41:22
回答 2查看 74关注 0票数 0

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

我引用了另一个例子,但是下面的错误不断出现。

代码语言:javascript
复制
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
代码语言:javascript
复制
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)

它不是在使用默认布局时自动膨胀吗?我哪里出错了?

item_custom_view

代码语言:javascript
复制
<?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>

模型

代码语言:javascript
复制
@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
    }
}

控制器

代码语言:javascript
复制
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)
        }
    }
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2022-07-05 12:22:44

使用lazy可以解决您的问题。

代码语言:javascript
复制
val textView: TextView by lazy{
     findViewById(R.id.title)
}
票数 1
EN

Stack Overflow用户

发布于 2022-10-16 13:26:18

您可以这样做来利用ViewBinding:

R.layout.item_custom_view:

代码语言:javascript
复制
<?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>
代码语言:javascript
复制
@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
    }}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72866433

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档