18
13

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Fragmentを表示した時にInfrateExeptionが出た時の対処

Last updated at Posted at 2013-01-30

TabUIの中にFragment、そのFragmentの中にbuttonとEditTextとListFragmentという構造のものを作ろうとしている時にInflateExceptionが出て大変だったのでメモ。
1回めの表示は問題ないのだが他タブを見てからそのタブに戻ろうとしたらInflateExceptionで死ぬ。

変更前

samplefragment.xml
<?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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <EditText
        android:id="@+id/SampleEdit"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="@string/queryHint"
        android:maxLines="1" >
    </EditText>

    <Button
        android:id="@+id/SampleButton"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/Load" >
    </Button>

      <fragment
        android:id="@+id/list"
        android:name="com.Android.test.TestListFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>

Fragmentを静的に置いてた。
この場合親がActivityとかだったら平気なんだけど親もFragmentなのでネストしたFragment となる。
ネストしたFragmentはサポートされるようになったが静的には生成出来ない。
必ず動的にしなければならないようだ。
NestedFragment

Note: You cannot inflate a layout into a fragment when that layout includes a . Nested fragments are only supported when added to a fragment dynamically.

ということで変更

変更後

samplefragment.xml
<?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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <EditText
        android:id="@+id/SampleEdit"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="@string/queryHint"
        android:maxLines="1" >
    </EditText>

    <Button
        android:id="@+id/SampleButton"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/Load" >
    </Button>

    <LinearLayout
        android:id="@+id/list_container"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
    </LinearLayout>/

</LinearLayout>

そしてFragment側で動的に呼び出す。

SampleFragment
public class SampleFragment1 extends Fragment {

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {      

        return inflater.inflate(R.layout.sample_fragment, container, false);
    }

    @Override
    public void onStart() {
        super.onStart();

        FragmentManager fm = getFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();
        ft.replace(R.id.list_container, new TestListFragment()).commit();
    }
    
}

呼び出す場所は適当にonStart()にしてるけどライフサイクル的にダメかもしれない...

多分当たり前なことなんですよねぇ...1回めは表示出来てしまったのでハマってしまった。
静的に呼び出すとIDだかタグが固定されちゃって2回め呼び出した時に重複して死ぬとかなんとか。
割りとハマったのでメモ代わりに。

18
13
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
18
13

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?