为什么我的适配器不工作?如何解决NullPointerException错误?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了为什么我的适配器不工作?如何解决NullPointerException错误?相关的知识,希望对你有一定的参考价值。
我正在构建新闻应用程序。我收到Kotlin空指针异常错误。我已经单击了频道名称列表,它将显示与那些频道来源不同的新闻,但是当我单击其中一个新闻频道时,它崩溃了。谁能帮我提供我的代码?这是它显示的错误:
FATAL EXCEPTION: main
Process: com.example.newswrld, PID: 16135
kotlin.KotlinNullPointerException
at com.example.newswrld.Adapter.ViewHolder.ListNewsAdapter.onBindViewHolder(ListNewsAdapter.kt:47)
at com.example.newswrld.Adapter.ViewHolder.ListNewsAdapter.onBindViewHolder(ListNewsAdapter.kt:16)
at androidx.recyclerview.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:7065)
at androidx.recyclerview.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:7107)
at androidx.recyclerview.widget.RecyclerView$Recycler.tryBindViewHolderByDeadline(RecyclerView.java:6012)
at androidx.recyclerview.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:6279)
at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:6118)
at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:6114)
at androidx.recyclerview.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2303)
at androidx.recyclerview.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1627)
at androidx.recyclerview.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1587)
at androidx.recyclerview.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:665)
at androidx.recyclerview.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:4134)
at androidx.recyclerview.widget.RecyclerView.dispatchLayout(RecyclerView.java:3851)
at androidx.recyclerview.widget.RecyclerView.onLayout(RecyclerView.java:4404)
at android.view.View.layout(View.java:20672)
at android.view.ViewGroup.layout(ViewGroup.java:6194)
at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1083)
at android.view.View.layout(View.java:20672)
at android.view.ViewGroup.layout(ViewGroup.java:6194)
at androidx.swiperefreshlayout.widget.SwipeRefreshLayout.onLayout(SwipeRefreshLayout.java:625)
at android.view.View.layout(View.java:20672)
at android.view.ViewGroup.layout(ViewGroup.java:6194)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
at android.view.View.layout(View.java:20672)
at android.view.ViewGroup.layout(ViewGroup.java:6194)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1812)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1656)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1565)
at android.view.View.layout(View.java:20672)
at android.view.ViewGroup.layout(ViewGroup.java:6194)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
at android.view.View.layout(View.java:20672)
at android.view.ViewGroup.layout(ViewGroup.java:6194)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1812)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1656)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1565)
at android.view.View.layout(View.java:20672)
at android.view.ViewGroup.layout(ViewGroup.java:6194)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
at com.android.internal.policy.DecorView.onLayout(DecorView.java:753)
at android.view.View.layout(View.java:20672)
at android.view.ViewGroup.layout(ViewGroup.java:6194)
at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2792)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2319)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1460)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:7183)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:949)
at android.view.Choreographer.doCallbacks(Choreographer.java:761)
at android.view.Choreographer.doFrame(Choreographer.java:696)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:935)
at android.os.Handler.handleCallback(Handler.java:873)
2020-06-05 18:05:23.297 16135-16135/com.example.newswrld E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
这是ListNewsAdapter.kt:
package com.example.newswrld.Adapter.ViewHolder
import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.example.newswrld.Common.ISO8601Parser
import com.example.newswrld.Interface.ItemClickListener
import com.example.newswrld.Model.Article
import com.example.newswrld.R
import com.squareup.picasso.Picasso
import java.text.ParseException
import java.util.*
class ListNewsAdapter(val articleList:MutableList<Article>, private val context: Context):RecyclerView.Adapter<ListNewsViewHolder>()
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ListNewsViewHolder
val inflater=LayoutInflater.from(parent!!.context)
val itemView=inflater.inflate(R.layout.news_layout,parent,false)
return ListNewsViewHolder(itemView)
override fun getItemCount(): Int
return articleList.size
override fun onBindViewHolder(holder: ListNewsViewHolder, position: Int)
Picasso.with(context)
.load(articleList[position].urlToImage)
.into(holder.article_image)
if(articleList[position].title!!.length > 65)
holder.article_title.text=articleList[position].title!!.substring(0,65)+"..."
else
holder.article_title.text=articleList[position].title!!
if(articleList[position].publishedAt!=null)
var date: Date?=null
try
date=ISO8601Parser.parse(articleList[position].publishedAt!!)
catch (ex:ParseException)
ex.printStackTrace()
holder.article_time.setReferenceTime(date!!.time)
holder.setItemClickListener(object :ItemClickListener
override fun onClick(view: View, position: Int)
)
这里是到源代码的链接:https://github.com/xSankeTx/NewsWRLD
答案
尝试更改此。
holder.setItemClickListener(object :ItemClickListener
override fun onClick(view: View, position: Int)
)
为此
holder.itemView.setOnClickListener
// do some action here
您的ViewHolder应该看起来像这样。
class ListSourceViewHolder(itemView:View): RecyclerView.ViewHolder(itemView)
以上是关于为什么我的适配器不工作?如何解决NullPointerException错误?的主要内容,如果未能解决你的问题,请参考以下文章
解决报错Failed to start bean ‘documentationPluginsBootstrapper‘; nested exception is java.lang.NullPoint
An internal error occurred during: "Map/Reducelocation status updater".java.lang.NullPoint