android.widget.RelativeLayout cannot be cast to android.widget.LinearLayout

谁学逆向工程 2016-12-29 02:56:56
java.lang.ClassCastException: android.widget.RelativeLayout cannot be cast to android.widget.LinearLayout

activity 主界面是这样的
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/scroll"
android:fillViewport="true"> <!-- 设置 ScrollView 的 android:fillViewport="true"就可以让下面的 LinearLayout充满屏幕-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="提交厨房"
android:id="@+id/TiJiaoChuFang"/>
<Button
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="删除选中项"
android:id="@+id/ShanChuXuanZhongXiang"/>
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/XuanZeDeCai">
</ListView>
</LinearLayout>
</ScrollView>

</LinearLayout>



ListView 中每个 item 是这样的
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="名称: "
android:textSize="20dp"
android:id="@+id/labMingCheng1"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20dp"
android:layout_toRightOf="@id/labMingCheng1"
android:id="@+id/labMingCheng2"/>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="删除"
android:id="@+id/delete"
android:layout_alignParentRight="true"
android:layout_below="@id/labMingCheng1"/>

<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/iFenShu"
android:textSize="20dp"
android:text="1"
android:layout_alignLeft="@id/labMingCheng2"
android:layout_below="@id/labMingCheng1"
android:layout_toLeftOf="@id/delete" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="份数: "
android:textSize="20dp"
android:id="@+id/labFenShu"
android:layout_below="@id/labMingCheng1"
android:layout_alignBottom="@id/iFenShu"
android:gravity="center"/>


</RelativeLayout>



ListView 还有个头
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<EditText
android:layout_width="match_parent"
android:layout_height="50dp"
android:hint="楼层"
android:id="@+id/LouCeng"/>
<EditText
android:layout_width="match_parent"
android:layout_height="50dp"
android:hint="房间"
android:id="@+id/FangJian"/>
<EditText
android:layout_width="match_parent"
android:layout_height="50dp"
android:hint="桌号"
android:id="@+id/ZhuoHao"
/>
</LinearLayout>


ListView 在代码中添加这个头
View HeaderView = View.inflate(this, R.layout.selectedmenuheader, null);
lv.addHeaderView(HeaderView);


最后我要循环遍历 ListView 中个每个 item
try {
LinearLayout layout = (LinearLayout) lv.getChildAt(i);
TextView tvName = (TextView) layout.findViewById(R.id.labMingCheng2);
EditText etFenShu = (EditText) layout.findViewById(R.id.iFenShu);
CheckBox cb = (CheckBox) layout.findViewById(R.id.delete);
}
catch (Exception ee)
{
Log.i("44445555", ee.toString());
}


总之,异常就出现在LinearLayout layout = (LinearLayout) lv.getChildAt(i);。
就算把 LinearLayout 换成 RelativeLayout 也一样转换异常,不能转换。
...全文
729 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
谁学逆向工程 2016-12-29
  • 打赏
  • 举报
回复
引用 1 楼 qq_34364155 的回复:
LinearLayout 换成 View 试试,可能是你头是LinearLayout,其他是RelativeLayout,遍历时就有一个会出错。

在 Android studio 中,logcat 和 debug 都在下面两个选项卡中,如果按下F8或者F9,窗口就自动跳到debug窗口,想看Log函数的输出还需要鼠标去点击 Android monitor,太麻烦了,怎么解决
谁学逆向工程 2016-12-29
  • 打赏
  • 举报
回复
引用 1 楼 qq_34364155 的回复:
LinearLayout 换成 View 试试,可能是你头是LinearLayout,其他是RelativeLayout,遍历时就有一个会出错。
不崩溃了,也不异常了
寒冰大神 2016-12-29
  • 打赏
  • 举报
回复
LinearLayout 换成 View 试试,可能是你头是LinearLayout,其他是RelativeLayout,遍历时就有一个会出错。

80,351

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧