我开发了一个应用程序,它使用 fragment 作为 fragment 的一部分。
public class LoginFragment extends Fragment
EditTextWithCameraButtonFrag userNameFrag;
EditTextWithCameraButtonFrag passwordFrag;
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
userNameFrag = (EditTextWithCameraButtonFrag)getChildFragmentManager()
.findFragmentById(R.id.fragment_username);
passwordFrag = (EditTextWithCameraButtonFrag) getChildFragmentManager()
.findFragmentById(R.id.fragment_password);
EditTextWithCameraButtonFrag
是 Fragment 的子类型。
这段代码就像一个魅力,运行在 android 5.1.1 上。
但是,在 Android 4.4.2 上运行它会返回 userNameFrage 和 passwordFrag 为 null。看来,返回的 Child FragmentManager 没有找到字段。
在 4.4.3 中使用 getChildFragmentManager()
时有什么需要考虑的吗?
提前致谢!
编辑: 这是主要 fragment 的 XML:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/huge"
android:orientation="vertical" >
<TextView
android:id="@+id/login_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/activity_vertical_margin"
android:gravity="center"
android:textColor="@color/white"
android:textSize="40dp" />
<fragment
android:id="@+id/fragment_username"
android:name="com.example.app.fragments.EditTextWithCameraButtonFrag"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<fragment
android:id="@+id/fragment_password"
android:name="com.example.app.fragments.EditTextWithCameraButtonFrag"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/login_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginEnd="@dimen/huge"
android:layout_marginStart="@dimen/huge"
android:layout_marginTop="@dimen/normal"
android:padding="@dimen/half"
android:text="Login" />
</LinearLayout>
这是子 fragment 的:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/editText_with_button_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<EditText
android:id="@+id/text_input_field"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:alpha="0.75"
android:background="@drawable/edit_text_login_top"
android:inputType="text"
android:padding="@dimen/normal" />
<Button
android:id="@+id/scan_text_view"
android:layout_width="wrap_content"
android:background="@android:color/transparent"
android:layout_height="wrap_content"
android:layout_alignEnd="@+id/text_input_field"
android:layout_alignTop="@+id/text_input_field"
android:layout_alignBottom="@+id/text_input_field"
/>
</RelativeLayout>
最佳答案
我刚刚解决了它 - 有点。 经过大量阅读后,似乎可以通过 XML 声明嵌套 fragment ,但这不是最佳实践。
所以我决定在 XML 中包含 FrameLayout
,并添加嵌套的
通过 FragmentTransaction
的 fragment 。
<RelativeLayout 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:gravity="center" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/huge"
android:orientation="vertical" >
<TextView
android:id="@+id/login_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/activity_vertical_margin"
android:gravity="center"
android:textColor="@color/white"
android:textSize="40dp" />
<FrameLayout
android:id="@+id/fragment_username"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<FrameLayout
android:id="@+id/fragment_password"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/login_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginEnd="@dimen/huge"
android:layout_marginStart="@dimen/huge"
android:layout_marginTop="@dimen/normal"
android:padding="@dimen/half"
android:text="Login" />
</LinearLayout>
</RelativeLayout>
在主 fragment 的 onCreate 中:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// create userNameFrag
EditTextWithCameraButtonFrag userfragment = new EditTextWithCameraButtonFrag();
getChildFragmentManager().beginTransaction().replace(R.id.fragment_username, userfragment).commit();
// create passwordFrag
EditTextWithCameraButtonFrag passfragment = new EditTextWithCameraButtonFrag();
getChildFragmentManager().beginTransaction().replace(R.id.fragment_password, passfragment).commit();
}
这似乎工作得很好而且 - 另一个优点 - 我也摆脱了 the duplicate id bug .
关于android - ChildFragmentManager.findFragmentByID() 在 android 4.4.2 上返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32373278/