java.lang.ClassCastException:android.widget.LinearLayout $ LayoutParams无法转换为android.support.v7.widget.RecyclerView $ LayoutParams

L3n*_*n95 5 java android classcastexception android-layout android-recyclerview

我有一个带有LinearLayout的RecyclerView。LinearLayout由9个按钮组成。在进行一些更改之前,我可以单击按钮,并且它没有崩溃。但是在我在下面添加此代码后,它给出了错误,并且没有显示它的来源:

java.lang.ClassCastException:android.widget.LinearLayout $ LayoutParams无法转换为android.support.v7.widget.RecyclerView $ LayoutParams

适配器中的导入:

    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;
Run Code Online (Sandbox Code Playgroud)

在onClickListener中添加了以下内容:

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

在RecyclerView.Adapter中添加了以下方法:

    public void grayButton(int row, int element){
    recycleViewDatas.get(row).setGray(element);
    notifyItemChanged(row);
}
Run Code Online (Sandbox Code Playgroud)

将此代码添加到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);
Run Code Online (Sandbox Code Playgroud)

这是我的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));
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

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" />
Run Code Online (Sandbox Code Playgroud)

...更多按钮

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>
Run Code Online (Sandbox Code Playgroud)

我不明白为什么更新后会出现错误。该应用程序仅在我的M9上崩溃。在HTC One V上,它不会崩溃,并且可以正常运行。你知道这个问题吗?您是否需要其他代码?

N.T*_*.T. 1

你的问题在于onBindViewHolder

LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
Run Code Online (Sandbox Code Playgroud)

LayoutParams为(几乎)所有布局实现。当你设置LayoutParams一个视图时,你需要用该视图的父视图的类型来设置它。因此,如果您有一个LinearLayout并且您正在尝试添加一个按钮,则需要LinearLayout.LayoutParamsButton. 这种情况下的问题是您不知道父类型。我的意思是它在不同的 Android 版本上可能会有所不同。

安全的选择是使用LayoutParams公共类(ViewGroup?)中的 ,因为您只设置宽度和高度。

  • 您的意思是使用: ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT); ?给我错误: android.view.ViewGroup$LayoutParams 无法转换为 android.support.v7.widget.RecyclerView$LayoutParams (2认同)