android - ClassCastException on layoutInflater.inflate(...) -


i keep getting error:

java.lang.classcastexception: android.widget.framelayout cannot cast android.widget.linearlayout 

whenever following executed:

@override public viewholder oncreateviewholder(viewgroup parent, int viewtype) {     layoutinflater layoutinflater = layoutinflater.from(parent.getcontext());     linearlayout mlinearlayout = (linearlayout) layoutinflater.inflate(r.layout.item, parent, false);     return new viewholder(mlinearlayout); } 

when makes no sense because xml file:

<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android"           android:orientation="vertical"           android:layout_width="match_parent"           android:layout_height="match_parent">  <textview     android:layout_width="wrap_content"     android:layout_height="wrap_content"     android:id="@+id/item_text_view"/>  </linearlayout> 

thus root of xml file should linearlayout, inflater should return linearlayout, getting framelayout?

replace

linearlayout mlinearlayout = (linearlayout) layoutinflater.inflate(r.layout.item, parent, false); return new viewholder(mlinearlayout); 

by

view view = layoutinflater.inflate(r.layout.item, parent, false); return new viewholder(view); 

edit

the public constructor of viewholder expects view not linearlayout. if need access child of layout, linearlayout have access it's findviewbyid, example:

your item layout

<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android"       android:id="@+id/your_linear_layout_id"       android:orientation="vertical"       android:layout_width="match_parent"       android:layout_height="match_parent">  <textview     android:layout_width="wrap_content"     android:layout_height="wrap_content"     android:id="@+id/item_text_view"/>  </linearlayout> 

your viewholder

public class exampleviewholder extends recyclerview.viewholder{      public exampleviewholder(view itemview) {         super(itemview);         linearlayout linearlayout = (linearlayout) itemview.findviewbyid(r.id.your_linear_layout_id);     } } 

oncreateviewholder

 @override  public viewholder oncreateviewholder(viewgroup parent, int viewtype) {     view view = layoutinflater.inflate(r.layout.item, parent, false);    return new viewholder(view);  } 

Comments