Итак, у меня есть два реселлера с разными адаптерами. Я хочу отобразить один из них, если я нажму кнопку. Другой адаптер работает хорошо. Но не со вторым адаптером.
корпус
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)
функция fetchJson
fun fetchJson(){
//Json for prev match
val url = "https://www.thesportsdb.com/api/v1/json/1/eventspastleague.php?id=4328"
val url2 = "https://www.thesportsdb.com/api/v1/json/1/eventsnextleague.php?id=4328"
val request = Request.Builder().url(url).build()
val request2 = Request.Builder().url(url2).build()
val client = OkHttpClient()
client.newCall(request).enqueue(object: Callback{
override fun onResponse(call: Call?, response: Response?){
println("sukses")
val body = response?.body()?.string()
println(body)
val gson = GsonBuilder().create()
val prevMatch = gson.fromJson(body, PrevMatch::class.java)
runOnUiThread { recyclerView_prevMatch.adapter = MainAdapters(prevMatch) }
}
override fun onFailure(call: Call?, e: IOException?){
println("failed request")
}
})
client.newCall(request2).enqueue(object: Callback{
override fun onResponse(call: Call?, response: Response?){
println("sukses")
val body = response?.body()?.string()
println(body)
val gson = GsonBuilder().create()
val nextMatch = gson.fromJson(body, NextMatch::class.java)
runOnUiThread { recyclerView_nextMatch.adapter = MainAdapter(nextMatch) }
}
override fun onFailure(call: Call?, e: IOException?){
println("failed request")
}
})
MainAdapter.kt file:
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
}
}
И когда я попытался запустить его, сила прекратилась. После того, как я смотрю на Logcat:
Process: com.example.android.footballmatchschedule, PID: 8307
java.lang.NullPointerException: Attempt to invoke interface method 'java.lang.Object java.util.List.get(int)' on a null object reference
at com.example.android.footballmatchschedule.MainAdapter.onBindViewHolder(MainAdapter.kt:24)
at com.example.android.footballmatchschedule.MainAdapter.onBindViewHolder(MainAdapter.kt:9)
at android.support.v7.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:6673)
at android.support.v7.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:6714)
at android.support.v7.widget.RecyclerView$Recycler.tryBindViewHolderByDeadline(RecyclerView.java:5647)
at android.support.v7.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:5913)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5752)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5748)
at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2232)
at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1559)
at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1519)
at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:614)
at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3812)
at android.support.v7.widget.RecyclerView.onMeasure(RecyclerView.java:3225)
at android.view.View.measure(View.java:19147)
at android.support.constraint.ConstraintLayout.internalMeasureChildren(ConstraintLayout.java:1212)
at android.support.constraint.ConstraintLayout.onMeasure(ConstraintLayout.java:1552)
at android.view.View.measure(View.java:19147)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6113)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:223)
at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:141)
at android.view.View.measure(View.java:19147)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6113)
at android.support.v7.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:400)
at android.view.View.measure(View.java:19147)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6113)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:223)
at android.view.View.measure(View.java:19147)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6113)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1723)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:788)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:648)
at android.view.View.measure(View.java:19147)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6113)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:223)
at com.android.internal.policy.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:3346)
at android.view.View.measure(View.java:19147)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2491)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1450)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1704)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1323)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6718)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:894)
at android.view.Choreographer.doCallbacks(Choreographer.java:696)
at android.view.Choreographer.doFrame(Choreographer.java:631)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:880)
at android.os.Handler.handleCallback(Handler.java:815)
at android.os.Handler.dispatchMessage(Handler.java:104)
at android.os.Looper.loop(Looper.java:207)
at android.app.ActivityThread.main(ActivityThread.java:5737)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller
Это показывает, что функция get (position) из API пытается получить ее из нулевого объекта. Как это может быть? Я имею в виду, как я могу решить это?