Android get(位置)显示在空引用上
所以,我有两个不同适配器的recyclerview。如果单击按钮,我想显示其中一个。另一个适配器工作正常。但不使用第二个适配器 身体Android get(位置)显示在空引用上,android,kotlin,android-recyclerview,Android,Kotlin,Android Recyclerview,所以,我有两个不同适配器的recyclerview。如果单击按钮,我想显示其中一个。另一个适配器工作正常。但不使用第二个适配器 身体 09-07 21:41:37.000 11177-11219/com.example.android.footballmatchschedule I/System.out: {"events":[{"idEvent":"576511","idSoccerXML":"389896","strEvent":"Huddersfield Town vs Crystal P
09-07 21:41:37.000 11177-11219/com.example.android.footballmatchschedule I/System.out: {"events":[{"idEvent":"576511","idSoccerXML":"389896","strEvent":"Huddersfield Town vs Crystal Palace","strFilename":"English Premier League 2018-09-15 Huddersfield Town vs Crystal Palace","strSport":"Soccer","idLeague":"4328","strLeague":"English Premier League","strSeason":"1819","strDescriptionEN":null,"strHomeTeam":"Huddersfield Town","strAwayTeam":"Crystal Palace","intHomeScore":null,"intRound":"5","intAwayScore":null,"intSpectators":null,"strHomeGoalDetails":null,"strHomeRedCards":null,"strHomeYellowCards":null,"strHomeLineupGoalkeeper":null,"strHomeLineupDefense":null,"strHomeLineupMidfield":null,"strHomeLineupForward":null,"strHomeLineupSubstitutes":null,"strHomeFormation":null,"strAwayRedCards":null,"strAwayYellowCards":null,"strAwayGoalDetails":null,"strAwayLineupGoalkeeper":null,"strAwayLineupDefense":null,"strAwayLineupMidfield":null,"strAwayLineupForward":null,"strAwayLineupSubstitutes":null,"strAwayFormation":null,"intHomeShots":null,"intAwayShots":null,"dateEvent":"2018-09-15","strDate":"15\/09\/18","strTime":"14:00:00+00:00","strTVStation":null,"idHomeTeam":"133932","idAwayTeam":"133632","strResult":null,"strCircuit":null,"strCountry":null,"strCity":null,"strPoster":null,"strFanart":null,"strThumb":null,"strBanner":null,"strMap":null,"strLocked":"unlocked"},{"idEvent":"576512","idSoccerXML":"389897","strEvent":"Man City vs Fulham","strFilename":"English Premier League 2018-09-15 Man City vs Fulham","strSport":"Soccer","idLeague":"4328","strLeague":"English Premier League","strSeason":"1819","strDescriptionEN":null,"strHomeTeam":"Man City","strAwayTeam":"Fulham","intHomeScore":null,"intRound":"5","intAwayScore":null,"intSpectators":null,"strHomeGoalDetails":null,"strHomeRedCards":null,"strHomeYellowCards":null,"strHomeLineupGoalkeeper":null,"strHomeLineupDefense":null,"strHomeLineupMidfield":null,"strHomeLineupForward":null,"strHomeLineupSubstitutes":null,"strHomeFormation":null,"strAwayRedCards":null,"strAwayYellowCards":null,"strAwayGoalDetails":null,"strAwayLineupGoalkeeper":null,"strAwayLineupDefense":null,"strAwayLineupMidfield":null,"strAwayLineupForward":null,"strAwayLineupSubstitutes":null,"strAwayFormation":null,"intHomeShots":null,"intAwayShots":null,"dateEvent":"2018-09-15","strDate":"15\/09\/18","strTime":"14:00:00+00:00","strTVStation":null,"idHomeTeam":"133613","idAwayTeam":"133600","strResult":null,"strCircuit":null,"strCountry":null,"strCity":null,"strPoster":null,"strFanart":null,"strThumb":null,"strBanner":null,"strMap":null,"strLocked":"unlocked"},{"idEvent":"576513","idSoccerXML":"389898","strEvent":"Newcastle vs Arsenal","strFilename":"English Premier League 2018-09-15 Newcastle vs Arsenal","strSport":"Soccer","idLeague":"4328","strLeague":"English Premier League","strSeason":"1819","strDescriptionEN":null,"strHomeTeam":"Newcastle","strAwayTeam":"Arsenal","intHomeScore":null,"intRound":"5","intAwayScore":null,"intSpectators":null,"strHomeGoalDetails":null,"strHomeRedCards":null,"strHomeYellowCards":null,"strHomeLineupGoalkeeper":null,"strHomeLineupDefense":null,"strHomeLineupMidfield":null,"strHomeLineupForward":null,"strHomeLineupSubstitutes":null,"strHomeFormation":null,"strAwayRedCards":null,"strAwayYellowCards":null,"strAwayGoalDetails":null,"strAwayLineupGoalkeeper":null,"strAwayLineupDefense":null,"strAwayLineupMidfield":null,"strAwayLineupForward":null,"strAwayLineupSubstitutes":null,"strAwayFormation":null,"intHomeShots":null,"intAwayShots":null,"dateEvent":"2018-09-15","strDate":"15\/09\/18","strTime":"14:00:00+00:00","strTVStation":null,"idHomeTeam":"134777","idAwayTeam":"133604","strResult":null,"strCircuit":null,"strCountry":null,"strCity":null,"strPoster":null,"strFanart":null,"strThumb":null,"strBanner":null,"strMap":null,"strLocked":"unlocked"},{"idEvent":"576515","idSoccerXML":"389900","strEvent":"Bournemouth vs Leicester","strFilename":"English Premier League 2018-09-15 Bournemouth vs Leicester","strSport":"Soccer","idLeague":"4328","strLeague":"English Premier League","strSeason":"1819","strDescriptionEN":null,"strHomeTeam":"Bournemout
NextMatch类别
class NextMatch(val event: List<Event>)
class Event(val strDate: String, val strHomeTeam: String, val strAwayTeam: String)
MainAdapter.kt文件:
class MainAdapter(val nextMatch: NextMatch): RecyclerView.Adapter<CustomViewHolder>(){
//Adapter Next Match
override fun getItemCount(): Int {
return 15
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CustomViewHolder {
val layoutInflater = LayoutInflater.from(parent.context)
val callForRow = layoutInflater.inflate(R.layout.item_row,parent,false)
return CustomViewHolder(callForRow)
}
override fun onBindViewHolder(holder: CustomViewHolder, position: Int) {
holder.view.team_home.text = nextMatch.event.get(position).strHomeTeam
holder.view.team_away.text = nextMatch.event.get(position).strAwayTeam
holder.view.time_schedule.text = nextMatch.event.get(position).strDate
}
}
它显示了API中的get(position)函数试图从null对象获取它。怎么可能呢?我的意思是如何解决这个问题?怀疑您的
NextMatch
是java定义的类型,其中事件可以为null。
尝试将onBindViewHolder
功能更改为以下内容
fun onBindViewHolder(holder: CustomViewHolder, position: Int) {
val event = nextMatch.event
if (event != null) {
holder.view.team_home.text = event.get(position).strHomeTeam
holder.view.team_away.text = event.get(position).strAwayTeam
holder.view.time_schedule.text = event.get(position).strDate
} else {
Log.d("TAG", "event is null!!")
}
}
如果您看到日志事件为空
,则确认您需要先在nextMatch
对象中设置事件
,然后再使用它
已更新
根据其他共享信息,问题是由于NextMatch
的event
变量与主体JSON名称events
不匹配造成的
因此,要解决这个问题,只需更改
class NextMatch(val event: List<Event>)
class NextMatch(val事件:列表)
到
class NextMatch(val事件:列表)
然后gson反序列化将起作用,其中事件现在不再为null。请怀疑您的NextMatch
是java定义的类型,其中事件可能为null。
尝试将onBindViewHolder
功能更改为以下内容
fun onBindViewHolder(holder: CustomViewHolder, position: Int) {
val event = nextMatch.event
if (event != null) {
holder.view.team_home.text = event.get(position).strHomeTeam
holder.view.team_away.text = event.get(position).strAwayTeam
holder.view.time_schedule.text = event.get(position).strDate
} else {
Log.d("TAG", "event is null!!")
}
}
如果您看到日志事件为空
,则确认您需要先在nextMatch
对象中设置事件
,然后再使用它
已更新
根据其他共享信息,问题是由于NextMatch
的event
变量与主体JSON名称events
不匹配造成的
因此,要解决这个问题,只需更改
class NextMatch(val event: List<Event>)
class NextMatch(val事件:列表)
到
class NextMatch(val事件:列表)
然后gson反序列化将起作用,其中事件现在不再为null。您的RecyclerView适配器的项目计数是常量,您确定吗
override fun getItemCount(): Int {
return 15
}
您的RecyclerView适配器的项目计数为常量,您确定吗
override fun getItemCount(): Int {
return 15
}
在onBindViewHolder
中运行代码之前,请检查nextMatch.event
是否不为空。删除帖子时要小心,您不希望以帖子禁令告终。可以我的错。我不知道,你却告诉我。非常感谢。欢迎使用@rayhanahaputra的副本。这就是我提到它的原因。在onBindViewHolder
中运行代码之前,请检查nextMatch.event
是否不为空。删除帖子时要小心,你不希望最后出现帖子禁令。可以我的错。我不知道,你却告诉我。非常感谢。欢迎使用@rayhanahaputra的副本。这就是我提到它的原因。是的,我看到了。从API获取Json函数是否有问题?可能是。因此,这个回收器视图或适配器没有问题。错误的地方是你的nextMatch
变量的设置。我已经在我的帖子中添加了这个函数。我不认为他们有什么问题,你能不能提供正文中的内容以及NextMatch
的格式是什么?找到问题,并提出解决方案,将事件更改为事件是的,我看到了日志。从API获取Json函数是否有问题?可能是。因此,这个回收器视图或适配器没有问题。错误的地方是你的nextMatch
变量的设置。我已经在我的帖子中添加了这个函数。我不认为他们有什么问题,你能不能提供正文中的内容以及NextMatch
的格式是什么?找到问题,并提出解决方案,将事件改为事件。我的错。我可以改为使用nextMatch.events.count()。谢谢你的提醒。真的很感激。在我看来这是正确的。这是OP遇到的问题的真正根源。当getItemCount()
返回正确的值时,无需检查null
!哦,是的。我的错。我可以改为使用nextMatch.events.count()。谢谢你的提醒。真的很感激。在我看来这是正确的。这是OP遇到的问题的真正根源。当getItemCount()
返回正确的值时,无需检查null
!