首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用PageAdapter绑定类的安卓?

使用PageAdapter绑定类的安卓?
EN

Stack Overflow用户
提问于 2021-02-08 02:44:55
回答 1查看 31关注 0票数 0

我的布局landing_item.xml

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

片段

代码语言:javascript
运行
复制
class LandingItemFragment : Fragment() {

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

适配器

代码语言:javascript
运行
复制
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时,它会崩溃,因为它找不到元素

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

回答 1

Stack Overflow用户

发布于 2021-02-08 05:03:48

您需要将项目布局包装在<layout>标记中

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

https://stackoverflow.com/questions/66091759

复制
相关文章

相似问题

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