안드로이드 fragment, 태그를 닫지 않으면 오류가 발생하는 이유는 뭘까요?

조회수 2573회

안드로이드 Fragment 관련 질문, 아니 넋두리입니다. 기가 막히네요... 정적으로 fragment 둘을 집어 넣을 activity 코드입니다.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="example.MainActivity">

    <fragment
        android:id="@+id/roadFragment"
        android:name="example.FragmentOne"
        tools:layout="@layout/fragment_one"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" />

    <fragment
        android:id="@+id/weatherFragment"
        android:name="example.FragmentTwo"
        tools:layout="@layout/fragment_two"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</LinearLayout>

이게 전혀 문제 없는 코드 아닌가요? 첫번째 frgament 항목에서 Error Inflating class fragment 오류가 발생해서 몇 시간을 해맸습니다. 유의미한 오류 메시지는 이게 전부라서...

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context android.support.v4.app.FragmentHostCallback.getContext()' on a null object reference

ContentProvider 문제인지, manifest 문제인지 별 짓을 다 해봤네요. 그리고 방금 코드를 다음과 같이 바꿨는데... (아무 생각 없이) 세상에 정상적으로 돌아갑니다. ㅠㅠ 왜 돌아가는지 도무지 모르겠어요. fragment 태그를 닫으니 정상적으로 돌아가는 이유가 대체 뭘까요??ㅠㅠ

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="example.MainActivity">

    <fragment
        android:id="@+id/roadFragment"
        android:name="example.FragmentOne"
        tools:layout="@layout/fragment_one"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >
    </fragment>

    <fragment
        android:id="@+id/weatherFragment"
        android:name="example.FragmentTwo"
        tools:layout="@layout/fragment_two"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
    </fragment>
</LinearLayout>
  • (•́ ✖ •̀)
    알 수 없는 사용자

1 답변

  • 태그를 </fragment>로 닫는것이나 </>이렇게 닫는것과 동일한건데요.

    에러 메시지를 보니 다른 곳에서 문제가 난것 같아요.

    지금은 코드가 잘 돌아간다고 하시니 </>  이렇게 하고 다시한번 실행해보세요. 올려주신 첫번째 코드로 제가 돌려봤을때는 정상적으로 돌아갔습니다. 다른부분을 고치신건 아닌지.. 다시한번 확인해보세요~

    • (•́ ✖ •̀)
      알 수 없는 사용자

답변을 하려면 로그인이 필요합니다.

프로그래머스 커뮤니티는 개발자들을 위한 Q&A 서비스입니다. 로그인해야 답변을 작성하실 수 있습니다.

(ಠ_ಠ)
(ಠ‿ಠ)