-2
RecyclerView多viewType錯誤安卓-kotlin -java.lang.ClassCastException
FATAL EXCEPTION: main
Process: com.example.expertsaraqeb.myapplication, PID: 4329
java.lang.ClassCastException: com.example.expertsaraqeb.myapplication.Madapter$MyHolder cannot be cast to com.example.expertsaraqeb.myapplication.MyViewH
06-05 07:00:35.897 4329-4329/com.example.expertsaraqeb.myapplication E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.expertsaraqeb.myapplication, PID: 4329
java.lang.ClassCastException: com.example.expertsaraqeb.myapplication.Madapter$MyHolder cannot be cast to com.example.expertsaraqeb.myapplication.MyViewH
at com.example.expertsaraqeb.myapplication.Madapter.onBindViewHolder(Madapter.kt:27)
at com.example.expertsaraqeb.myapplication.Madapter.onBindViewHolder(Madapter.kt:11)
at android.support.v7.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:6356)
at android.support.v7.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:6389)
at android.support.v7.widget.RecyclerView$Recycler.tryBindViewHolderByDeadline(RecyclerView.java:5335)
at android.support.v7.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:5598)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5440)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5436)
at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2224)
at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1551)
at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1511)
at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:595)
at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3583)
at android.support.v7.widget.RecyclerView.onMeasure(RecyclerView.java:3025)
at android.view.View.measure(View.java:16749)
at android.support.constraint.ConstraintLayout.internalMeasureChildren(ConstraintLayout.java:934)
at android.support.constraint.ConstraintLayout.onMeasure(ConstraintLayout.java:973)
at android.view.View.measure(View.java:16749)
at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1658)
at android.view.View.measure(View.java:16749)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5295)
at android.support.design.widget.CoordinatorLayout.onMeasureChild(CoordinatorLayout.java:714)
at android.support.design.widget.HeaderScrollingViewBehavior.onMeasureChild(HeaderScrollingViewBehavior.java:90)
at android.support.design.widget.AppBarLayout$ScrollingViewBehavior.onMeasureChild(AppBarLayout.java:1391)
at android.support.design.widget.CoordinatorLayout.onMeasure(CoordinatorLayout.java:784)
at android.view.View.measure(View.java:16749)
at android.support.v4.widget.DrawerLayout.onMeasure(DrawerLayout.java:1081)
at android.view.View.measure(View.java:16749)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5295)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:139)
at android.view.View.measure(View.java:16749)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5295)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1413)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:696)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:589)
at android.view.View.measure(View.java:16749)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5295)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at android.view.View.measure(View.java:16749)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5295)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1413)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:696)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:589)
at android.view.View.measure(View.java:16749)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5295)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2439)
at android.view.View.measure(View.java:16749)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2072)
at android.
代碼:
package com.example.expertsaraqeb.myapplication
import android.content.Context
import android.support.v7.widget.RecyclerView
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
class Madapter (val context: Context, val array:ArrayList<ModelItem>): RecyclerView.Adapter<Madapter.MyHolder>() {
var TYPE1=15
var TYPE2=28
override fun getItemViewType(position: Int): Int {
if (position==0){
return TYPE1
}else{
return TYPE2
}
}
override fun getItemCount(): Int {
return array.size
}
override fun onBindViewHolder(p0: MyHolder?, p1: Int) {
if (p0!!.itemViewType==TYPE1){
val my= p0 as MyViewH //Error here
Log.d("walidalhasn","p"+getItemViewType(p1))
}else if (p0.itemViewType==TYPE1){
Log.d("walidalhasn","j"+getItemViewType(p1))
}
}
override fun onCreateViewHolder(p0: ViewGroup?, p1: Int): MyHolder {
if (getItemViewType(p1)==TYPE1){
val view= LayoutInflater.from(context).inflate(R.layout.row_no_color,p0,false)
return MyViewH(view)
}else {
val view= LayoutInflater.from(context).inflate(R.layout.home_row,p0,false)
return MyHolder(view)
}
}
open class MyHolder (view: View):RecyclerView.ViewHolder(view)
}
class MyViewH(view: View?) : Madapter.MyHolder(view!!)
謝謝你的答案,但它不工作 –
'致命異常:主要 過程:com.alhasnapps.resimleri,PID:17516 kotlin.TypeCastException:空不能轉換到非空型android.widget。 ImageView ' –
我在代碼中看不到與ImageView相關的任何內容。 –