java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams cannot be cast to android.support.v7.widget.RecyclerView$LayoutParams
Asked Answered
H

3

7

I have a RecyclerView with a LinearLayout inside. The LinearLayout consists out of 9 buttons. Before some changes I could click the buttons and it didn't crashed. But after I added this code below it gives the error and does not show where it comes from:

java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams cannot be cast to android.support.v7.widget.RecyclerView$LayoutParams

The imports 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 the onClickListener:

tempTag = (int) v.getTag();
mAdapter.grayButton(tempTag / 9, tempTag % 9);
zahl1 = Integer.parseInt(mAdapter.getText(tempTag).toString());

Added this method in the RecyclerView.Adapter:

    public void grayButton(int row, int element){
    recycleViewDatas.get(row).setGray(element);
    notifyItemChanged(row);
}

Added this code in the 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));
        }
    }
}

The layout file of the 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

The layout of the 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 do not understand why the error comes after the update. And the app only crashes on my M9. On the HTC One V it does not crash and all works as expected. You know the problem? Do you need any more code?

Hildegardhildegarde answered 7/8, 2015 at 11:38 Comment(14)
The problem is stated in the title... You use 2 different LayoutParams, one standard, and one from the v7 support library... Try to use 1 instead... choose the one depending on the further needReproof
But where do I use 2 different? In Main and in Adapter I import import android.support.v7.widget.RecyclerView; import android.view.ViewGroup.LayoutParams;Hildegardhildegarde
LinearLayout in ViewHolder, what are you importing?Reproof
For the LinearLayout in ViewHolder? I import import android.widget.LinearLayout;Hildegardhildegarde
Where do you call the LayoutParams? Can you show that including the imports?Reproof
Look edit above in onBindViewHolderHildegardhildegarde
What is the parent of your linear layout in the list item layout(viewHolder.ll)?Enjambement
The LinearLayout is the parent. Added the layout file above.Hildegardhildegarde
You need to set the immediate parent layout params for any viewEnjambement
So change the import of 'android.support.v7.widget.RecyclerView.LayoutParams' to 'android.view.ViewGroup.LayoutParams'Enjambement
Then the error is that ViewGroup$LayoutParams cannot be cast to android.v7...RecyclerView$LayoutParamsHildegardhildegarde
change the RecyclerView$LayoutParams to ViewGroup$LayoutParams, or vice versaReproof
change import android.support.v7.widget.RecyclerView.LayoutParams; to android.widget.LinearLayout.LayoutParams (guessing on the first packaged, but note the last 2, and the not v7)Reproof
If I comment out the 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); in the onBindVIewHolder it does not crash.Hildegardhildegarde
W
1

Your problem is in onBindViewHolder:

LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);

LayoutParams is implemented for (almost) all the layouts. When you set LayoutParams on a view, you need to set it with a type of the view's parents. So if you have a LinearLayout and you are trying to add a Button, you need to set LinearLayout.LayoutParams on the Button. The problem in this case is that you don't know the parent type. And by that I mean it can different on different Android versions.

The safe bet is to use the LayoutParams from a common class (ViewGroup?) since you only set width and height.

Wildermuth answered 7/8, 2015 at 12:31 Comment(2)
You mean to use: ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);? Gives me the error: android.view.ViewGroup$LayoutParams cannot be cast to android.support.v7.widget.RecyclerView$LayoutParamsHildegardhildegarde
What happens if you force the cast?Wildermuth
B
1

i had same problem, you can use this

ViewGroup.LayoutParams layoutParams =v.itemView.getLayoutParams();
    layoutParams.width= ViewGroup.LayoutParams.MATCH_PARENT;
    layoutParams.height= 200;
    v.itemView.setLayoutParams(layoutParams);
Bova answered 5/8, 2018 at 21:6 Comment(0)
Q
1

if you need the kotlin version :

 with(view.layoutParams){
                width = ViewGroup.LayoutParams.WRAP_CONTENT
                height = ViewGroup.LayoutParams.MATCH_PARENT
 }
Quitrent answered 10/3, 2020 at 13:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.