java. lang.ClassCastException:android. widget. LinearLayout $layoutparams cannot be cast to Android support. v7. widget. RecyclerView $LayoutParams
I have a recyclerview with LinearLayout LinearLayout consists of nine buttons Before making some changes, I can click the button and it doesn't crash However, after I add the following code, it will give an error and will not show where it comes from:
Import in adapter:
import android.content.Context; import android.graphics.Color; import android.support.v7.widget.RecyclerView; import android.widget.LinearLayout.LayoutParams; import android.util.TypedValue; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.Button; import android.widget.LinearLayout; import android.widget.TextView;
Added this in onclicklistener:
tempTag = (int) v.getTag(); mAdapter.grayButton(tempTag / 9,tempTag % 9); zahl1 = Integer.parseInt(mAdapter.getText(tempTag).toString());
In recyclerview This method is added to the adapter:
public void grayButton(int row,int element){ recycleViewDatas.get(row).setGray(element); notifyItemChanged(row); }
The following code has been added to onbindviewholder:
LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT); viewHolder.ll.setLayoutParams(lp); lp.height = viewHolder.ll.getLayoutParams().height - (int) (viewHolder.buttons.get(j).getLayoutParams().height - recycleViewDatas.get(i).getSize()) + 30; viewHolder.ll.setLayoutParams(lp); if(recycleViewDatas.get(i).getGray() == j) viewHolder.buttons.get(j).setTextColor(Color.GRAY); else if(recycleViewDatas.get(i).getGray() == -1) viewHolder.buttons.get(j).setTextColor(Color.BLACK);
This is my viewholder:
public static class ViewHolder extends RecyclerView.ViewHolder { LinearLayout ll; private ArrayList<Button> buttons = new ArrayList<>(); public ViewHolder(View v,Context context) { super(v); ll = (LinearLayout) v.findViewById(R.id.llRecycleView); for (int i = 0; i < ll.getChildCount(); i++) { buttons.add((Button) ll.getChildAt(i)); } } }
Layout file of recycleviewdata:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content" android:weightSum="9" android:background="@drawable/zeile" android:id="@+id/llRecycleView"> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:textColor="@android:color/black" android:background="@android:color/transparent" android:textSize="23sp" android:clickable="false" android:layout_gravity="center" android:gravity="center" />
... more buttons
Layout of mainactivity:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:orientation="vertical" android:layout_width="match_parent" android:layout_height="wrap_content" android:weightSum="2" android:background="@drawable/top"> ...some buttons </LinearLayout> <android.support.v7.widget.RecyclerView android:id="@+id/my_recycler_view" android:scrollbars="vertical" android:layout_width="match_parent" android:layout_height="match_parent"/></LinearLayout>
I don't understand why there are errors after the update The application only crashes on my M9 On HTC one V, it doesn't crash and everything works as expected Do you know the problem? Do you need more code?
Solution
Your problem is on the onbindviewholder:
LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT);
Layoutparams is implemented for (almost) all layouts When setting layoutparams on a view, you need to set it using the parent type of the view Therefore, if you have a LinearLayout and you try to add a button, you need to set LinearLayout on the button LayoutParams. The problem in this case is that you don't know the parent type I think it can be different in different Android versions
The safe bet is to use the public class (ViewGroup?) Because you only set the width and height