gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 11:06:52 24 4
gpt4 key购买 nike

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

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

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;

在 onClickListener 中添加:

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

在 RecyclerView.Adapter 中添加了这个方法:

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

在 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);

这是我的 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));
}
}
}

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" />

...更多按钮

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>

我不明白为什么更新后会出现错误。该应用程序仅在我的 M9 上崩溃。在 HTC One V 上,它不会崩溃,并且一切正常。你知道问题所在吗?您需要更多代码吗?

最佳答案

您的问题在 onBindViewHolder 中:

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

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

安全的做法是使用普通类(ViewGroup?)中的 LayoutParams,因为您只设置了宽度和高度。

关于java.lang.ClassCastException : android. widget.LinearLayout$LayoutParams 无法转换为 android.support.v7.widget.RecyclerView$LayoutParams,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31876833/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com