[Android] Fragment를 layout 파일에서 적용시 InflateException 오류 발생

Fragment를 이용한 화면 구성 시 Java나 kotlin코드상에서 FragmentManager를 활용하는 방법 외의 아래처럼 레이아웃 파일에 직접 구성하는 방법이 있다.

<LinearLayout
            android:id="@+id/layout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="horizontal"
            android:visibility="gone">

            <fragment
                android:id="@+id/customFragment"
                android:name="com.dev.guy"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="9" />
</LinearLayout>


위와 같은 방법 사용시 간헐적으로 아래의 오류를 뱉어내며 예외가 발생하는 경우가 있을 것이다.


InflateException

Binary XML file line #23 in com.dev.guy:layout/customlayout: Error inflating class fragment


정확하게 발생 경로를 발견하지는 못했으나 스택오버플로를 통해 아래처럼 해결하면 된다는 소스를 얻어서 확인 중이다.

<LinearLayout
            android:id="@+id/layout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="horizontal"
            android:visibility="gone">

            <fragment
                android:id="@+id/customFragment"
                class="com.dev.guy"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="9" />
</LinearLayout>


참고 : https://stackoverflow.com/questions/19874882/android-view-inflateexception-binary-xml-file-error-inflating-class-fragment






댓글