com.android.layoutlib.bridge.MockView cannot be cast to android.view.ViewGroup
ListView 內部測量/佈局 Item 時,會將 convertView 傳遞給 getView。若 Item 根佈局為 RelativeLayout,某些舊版構建工具(ADT/Eclipse + 舊 SDK)在預渲染階段會生成 MockView 而非真實 ViewGroup,導致後續 findViewById 或佈局參數轉換失敗。
getView 中為 convertView 設置 AbsListView.LayoutParams。<LinearLayout xmlns:android="..."
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView android:id="@+id/left_text"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content" />
<TextView android:id="@+id/right_text"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:gravity="end" />
</LinearLayout>
使用 layout_weight 實現左右分佈,無需嵌套多層佈局,性能更優。
ListView,改用 RecyclerView + ViewHolder 模式,彈性更強、崩潰率更低。