【发布时间】:2016-08-01 19:26:45
【问题描述】:
我不断收到错误:
java.lang.ClassCastException: android.widget.FrameLayout cannot be cast to android.widget.LinearLayout
每当执行以下操作时:
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
LinearLayout mLinearLayout = (LinearLayout) layoutInflater.inflate(R.layout.item, parent, false);
return new ViewHolder(mLinearLayout);
}
什么时候没有意义,因为这是我的 xml 文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/item_text_view"/>
</LinearLayout>
所以XML文件的根应该是LinearLayout,所以inflater应该返回LinearLayout,它从哪里得到FrameLayout?
【问题讨论】:
-
这是布局文件夹中名为
item的文件的xml吗? -
是的。 [15 个字符]
-
为什么需要投射?只需使用
View v = inflater.inflate -
您的布局中没有 ID 为
item的孩子 -
为什么您的
ViewHolder需要LinearLayout?View怎么了?
标签: android adapter layout-inflater