Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/205.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 在RecyclerView.Adapter中绑定视图时出现NullPointerException_Android_Kotlin_Android Recyclerview_Findviewbyid - Fatal编程技术网

Android 在RecyclerView.Adapter中绑定视图时出现NullPointerException

Android 在RecyclerView.Adapter中绑定视图时出现NullPointerException,android,kotlin,android-recyclerview,findviewbyid,Android,Kotlin,Android Recyclerview,Findviewbyid,为什么在我的ViewHolder的bindItems()方法中会出现NullPointerException 我突出显示了获取NullPointerException的那一行。正如您在XML中看到的那样,blogpost\u作者的ID是存在的,那么这里有什么问题呢?findViewById(R.id.blogpost\u author)如何返回null Adapter和ViewHolder代码: class BlogPostAdapter(val blogList: ArrayList<B

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

我突出显示了获取
NullPointerException
的那一行。正如您在XML中看到的那样,
blogpost\u作者的ID是存在的,那么这里有什么问题呢?
findViewById(R.id.blogpost\u author)
如何返回null

Adapter
ViewHolder
代码:

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
        }
    }
}
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)
用于回收视图的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>

您可能在您的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)

你试过val recyclerView=findViewById(R.id.recyclerView)作为RecyclerViewHah的可能副本吗,我不敢相信是这个!事实上,我指的是第二个布局xml,而不是recyclerview布局。一个更好的命名约定会帮助我发现这一点!谢谢,迪恩。
val blogPostAuthor = itemView.findViewById<TextView>(R.id.blogpost_author)
val v = LayoutInflater.from(parent.context).inflate(R.layout.your_card_layout, parent, false)