java.lang.ClassCastException:android.widget.LinearLayout $L
我有一个带有LinearLayout的RecyclerView. LinearLayout由9个按钮组成.在进行一些更改之前,我可以单击按钮并且它没有崩溃.但是在我添加下面的代码后,它会给出错误并且不会显示它来自何处:
适配器中的导入: 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是为(几乎)所有布局实现的. 安全的赌注是使用公共类(ViewGroup?)中的LayoutParams,因为您只设置宽度和高度. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- Struts2 ActionContext.getContext()方法:获取ActionConte
- java – 使用ScalaObjectMapper的Jackson模块在Spark 1.4.0
- java – 如果int不继承Object,那么为什么“String.format(S
- java反射机制示例
- java – 用Byte改变位值
- android日志打印工具类
- java – 可以获取Hibernate sqlRestriction的连接表的SQL别
- java – 我可以从jar /多个模式加载flyway迁移
- 如何在java循环中设置一个switch语句
- 详解Java无需解压直接读取Zip文件和文件内容