android - 在 RecyclerView.Adapter 中绑定(bind) View 时出现 NullPointerException

标签 android kotlin android-recyclerview findviewbyid

为什么我在 NullPointerExceptionViewHolder 方法中得到一个 bindItems()

我已经突出显示了我获得 NullPointerException 的行。 blogpost_author ID 存在,正如您在 XML 中看到的那样,那么这里有什么问题呢? findViewById<TextView>(R.id.blogpost_author) 是如何返回 null 的?

AdapterViewHolder代码:

class BlogPostAdapter(val blogList: ArrayList<BlogPost>) : RecyclerView.Adapter<BlogPostAdapter.ViewHolder>() {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int) : BlogPostAdapter.ViewHolder {
        val v = LayoutInflater.from(parent.context).inflate(R.layout.blog_post_list, parent, false)
        return ViewHolder(v)
    }

    override fun getItemCount(): Int {
        return blogList.size
    }

    override fun onBindViewHolder(holder: BlogPostAdapter.ViewHolder, position: Int) {
        holder.bindItems(blogList[position])
    }

    class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        fun bindItems(blogPost: BlogPost) {
            val blogPostAuthor = itemView.findViewById<TextView>(R.id.blogpost_author) // THIS LINE - NULL POINTER EXCEPTION
            val blogPostTitle = itemView.findViewById<TextView>(R.id.blogpost_title)
            blogPostAuthor.text = blogPost.author
            blogPostTitle.text = blogPost.title
        }
    }
}

Activity代码:

class BlogPostListActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.blog_post_list)

        // Get the RecyclerView from XML itself
        val recyclerView = findViewById<RecyclerView>(R.id.recyclerview)

        // Add a layout manager - What does a layout manager do?
        recyclerView.layoutManager = LinearLayoutManager(this, LinearLayout.VERTICAL, false)

        // Create an array list to store blogposts using the the data class blogPost
        val blogPosts = ArrayList<BlogPost>()

        // Add some dummy data to the list
        blogPosts.add(BlogPost(123, "First Blog Post", "John"))
        blogPosts.add(BlogPost(456, "Second Blog Post", "Bob"))
        blogPosts.add(BlogPost(789, "Third Blog Post", "Mary"))

        // Create an adapter
        val adapter = BlogPostAdapter(blogPosts)

        // Add the adapter to the recyclerview
        recyclerView.adapter = adapter
    }
}

Kotlin 数据类:

data class BlogPost(val id: Int, val title: String, val author: String)

RecyclerView 的 XML:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.topzap.android.kotlinlistapptest.BlogPostListActivity">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:layout_editor_absoluteX="8dp"
        tools:layout_editor_absoluteY="8dp"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"/>
</android.support.constraint.ConstraintLayout>

CardView 布局的 XML:

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

    <android.support.v7.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

        <TextView
            android:id="@+id/blogpost_author"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="5dp"
            android:text="AuthorPlaceHolder"
            android:textAppearance="@style/Base.TextAppearance.AppCompat.Large"
            />

        <TextView
            android:id="@+id/blogpost_title"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="5dp"
            android:text="TitlePlaceHolder"
            android:textAppearance="@style/Base.TextAppearance.AppCompat.Medium"
            />
        </LinearLayout>
    </android.support.v7.widget.CardView>
</LinearLayout>

最佳答案

您可能在 RecyclerView 中填充了错误的布局。

onCreateViewHolder 方法中的这一行:

val v = LayoutInflater.from(parent.context).inflate(R.layout.blog_post_list, parent, false)

您正在扩充 blog_post_list.xml,我假设这是错误的布局文件,因为您还在此处的 BlogPostListActivity 中扩充了该布局:

setContentView(R.layout.blog_post_list)

所以当这一行被调用时:

val blogPostAuthor = itemView.findViewById<TextView>(R.id.blogpost_author)

它正在 R.layout.blog_post_list 中查找 ID“blogpost_author”,正如您所见,该布局中没有 blogpost_author TextView,因此它返回 null。

要解决这个问题,它应该是直截了当的,只需更改您在 onCreateViewHolder 方法中分配给每个 ViewHolder 的布局资源,并使用适合您的 CardView 布局的正确布局。

这意味着该行应该是这样的:

val v = LayoutInflater.from(parent.context).inflate(R.layout.your_card_layout, parent, false)

关于android - 在 RecyclerView.Adapter 中绑定(bind) View 时出现 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46525690/

相关文章:

kotlin - Mockito:在Kotlin中模拟khttp对象

android - 从字符串中获取android的id资源

android - 无法更新 RecyclerView 中的 TextView 字段

android - 具有动态项目高度的水平 RecyclerView

java - AdMob 显示状态为无效

android - 为什么 Google 选择了 RenderScript 而不是 OpenCL

java - 在 Android 12 上使用 Huawei HMS Base 6.1.0.302 执行针对 API 31 的 Android APP 时崩溃

android - Grid Recyclerview,偶数行有 3 列,奇数行有 4 列

android - 口味 - 找不到与包名称匹配的客户端

android - 折叠工具栏锚定 View 在折叠时隐藏