【问题标题】:Android using Binding classes with PageAdapter?Android 使用带有 PageAdapter 的绑定类?
【发布时间】:2021-02-07 18:44:55
【问题描述】:

我的布局landing_item.xml

<androidx.appcompat.widget.LinearLayoutCompat
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".landing.MainActivity">

    <com.google.android.material.textview.MaterialTextView
        android:id="@+id/title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

    <com.google.android.material.textview.MaterialTextView
        android:id="@+id/description"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

    <ImageView
        android:id="@+id/image"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

</androidx.appcompat.widget.LinearLayoutCompat>

片段

class LandingItemFragment : Fragment() {

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View = inflater.inflate(R.layout.landing_item, container, false)
}

适配器

class LandingItemPageAdapter(fa: FragmentActivity) : FragmentStateAdapter(fa) {
    override fun getItemCount(): Int = 3

    override fun createFragment(position: Int): Fragment = LandingItemFragment()

    override fun onBindViewHolder(
        holder: FragmentViewHolder,
        position: Int,
        payloads: MutableList<Any>
    ) {
        super.onBindViewHolder(holder, position, payloads)
        val binder = LandingItemBinding.bind(holder.itemView)
        binder.title.text = "Test"
        binder.description.text = "This is a test"
    }
}

调用 bind 时会因为找不到元素而崩溃

2021-02-07 10:40:05.183 8904-8904/com.comp.app E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.comp.app, PID: 8904
    java.lang.NullPointerException: Missing required view with ID: com.comp.app/description
        at com.comp.app.databinding.LandingItemBinding.bind(LandingItemBinding.java:88)

【问题讨论】:

    标签: android android-viewbinding


    【解决方案1】:

    您需要将项目布局包装在&lt;layout&gt;标签中

    <layout xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:tools="http://schemas.android.com/tools">
    
        <androidx.appcompat.widget.LinearLayoutCompat
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            tools:context=".landing.MainActivity">
    
            <com.google.android.material.textview.MaterialTextView
                android:id="@+id/title"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"/>
    
            <com.google.android.material.textview.MaterialTextView
                android:id="@+id/description"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"/>
    
            <ImageView
                android:id="@+id/image"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"/>
    
        </androidx.appcompat.widget.LinearLayoutCompat>
    
    </layout>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-11-08
      • 1970-01-01
      • 2020-07-29
      • 2017-08-09
      • 1970-01-01
      • 2018-08-13
      • 1970-01-01
      • 2017-10-29
      相关资源
      最近更新 更多