在RecyclerView.Adapter中绑定Views时出现NullPointerException

3

为什么在我的ViewHolderbindItems()方法中会出现NullPointerException

我已经用高亮标记了出现NullPointerException的行。正如您在XML中所看到的,blogpost_author ID是存在的,那么问题出在哪里呢?为什么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>

你试过这样写吗? val recyclerView = findViewById(R.id.recyclerview) as RecyclerView - Geeta Gupta
可能是什么是NullPointerException,如何修复它?的重复问题。 - Dima Kozhevin
1个回答

6
你可能在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)

1
哈哈,真不敢相信是这个问题!我确实指向了第二个布局xml而不是recyclerview布局。更好的命名规范会帮助我发现这个问题!谢谢Dean。 - Xarsiss

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接