首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> 更多程式語言 >> 程式設計綜合問答

Android LinearLayout 膨脹異常:XML 標籤大小寫敏感導致

     編輯:程式設計綜合問答

錯誤日志

android.view.InflateException: Binary XML file line #1: Error inflating class Linearlayout
Caused by: java.lang.ClassNotFoundException: Didn't find class "android.view.Linearlayout"

根因

Android XML 布局中標籤名區分大小寫,必須與類名完全一致。
正確:<LinearLayout>(L 大寫)
錯誤:<Linearlayout>(l 小寫)

修正示例

<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">

    <LinearLayout
        android:id="@+id/left"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:background="#CCCCCC"
        android:orientation="vertical">
        <Button ... />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/center"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:background="#CCDDFF"
        android:orientation="vertical">
    </LinearLayout>

    <LinearLayout
        android:id="@+id/right"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:background="#CCFFDD"
        android:orientation="vertical">
    </LinearLayout>
</LinearLayout>

補充建議

  • 使用 layout_weight 分配寬度,而非寫死 0dp 且無權重。
  • 巢狀過深影響性能,考慮 ConstraintLayout 扁平化。
  • IDE 提示紅線時滑鼠懸停可直接查看「Class not found」提示。
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved