2016年10月17日月曜日

Fragment使用findFragmentById返回null

Fragment使用findFragmentById返回null

findFragmentById 好像只能用在XML内添加的Fragment。如下

    <LinearLayout
        <fragment
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:name="com.example.e560.m1016a.FL"
            android:id="@+id/fragment" />
    </LinearLayout>


否则只能用 getFragmentManager().findFragmentByTag。
        fm = getFragmentManager();
        ft = fm.beginTransaction();
        FL fl = new FL();
        ft.add(R.id.a, fl, "fl");
        ft.commit();

        FL  ff = (FL) getFragmentManager().findFragmentByTag("fl");
        FL  ff2 = (FL) getFragmentManager().findFragmentById(R.id.fragment);


 I/System.out: FL{527fd414 #1 id=0x7f0b0055 fl}
 I/System.out: FL{5280f1c0 #0 id=0x7f0b0056}

0 件のコメント: