将片段添加到对话框片段
Adding fragment to a dialog fragment
我有一个布局需要基于 strInfo 值的片段。
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = getActivity().getLayoutInflater().inflate(R.layout.activity_onboarding, null);
Bundle bundle = getArguments();
strInfo = bundle.getString(S.SP_USER_INFO);
//Lot of code regarding the view
return view;
}
我认为片段的视图应该添加到 onViewCreated 中,因为片段的容器应该首先膨胀。
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
int infoCount = 1;
switch (strInfo){
case S.UserInfoPrefs.GOAL:
S.L("AOPOOP");
infoCount = 1;
getFragmentManager().beginTransaction().add(R.id.aonboarding_fl_inputs, new TestFragment()).commit();
break;
case S.UserInfoPrefs.WEIGHT:
infoCount = 3;
S.L("ASFLGH");
//getFragmentManager().beginTransaction().add(R.id.aonboarding_fl_inputs, new WeightFragment()).commit();
break;
}
}
但是我收到这个错误:
java.lang.IllegalArgumentException: No view found for id 0x7f0f00a9
(in.jiyofit.newjiyofit:id/aonboarding_fl_inputs) for fragment TestFragment
我将 getFragmentManager 放在 postDelayed 线程中。 activity_onboarding 布局变得很好。但我仍然收到错误。
这是 TestFragment 的 xml:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="TEST"
android:gravity="center"/>
</LinearLayout>
XML 父布局
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:orientation="vertical"
android:gravity="center_horizontal"
android:layout_weight="0.15">
//some code
</LinearLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.35"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:gravity="center">
//some code
</RelativeLayout>
<FrameLayout
android:id="@+id/aonboarding_fl_inputs"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.3"/>
<Button
android:id="@+id/aonboarding_btn_proceed"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.1"
android:text="@string/submit"
style="@style/ProceedButtonStyle"/>
getFragmentManager().beginTransaction().add(R.id.aonboarding_fl_inputs, new TestFragment()).commit();
用以下几行更改上面的行。
this.getChildFragmentManager().beginTransaction().add(R.id.aonboarding_fl_inputs, new TestFragment());
t.commit();
注释:
当您从 activity 添加或替换 fragment
时,您可以直接使用您的 activity 片段管理器,即 getFragmentManager
但是当您需要添加/替换片段时从片段布局然后引用 getFragmentManager()
将使代码从父 activity 的 xml 而不是片段 xml 中查找。因此它不会在那里找到任何引用您提供的 ID 的视图,应用程序将崩溃。
您需要使用 getChildFragmentManager()
来引用当前片段。
我有一个布局需要基于 strInfo 值的片段。
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = getActivity().getLayoutInflater().inflate(R.layout.activity_onboarding, null);
Bundle bundle = getArguments();
strInfo = bundle.getString(S.SP_USER_INFO);
//Lot of code regarding the view
return view;
}
我认为片段的视图应该添加到 onViewCreated 中,因为片段的容器应该首先膨胀。
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
int infoCount = 1;
switch (strInfo){
case S.UserInfoPrefs.GOAL:
S.L("AOPOOP");
infoCount = 1;
getFragmentManager().beginTransaction().add(R.id.aonboarding_fl_inputs, new TestFragment()).commit();
break;
case S.UserInfoPrefs.WEIGHT:
infoCount = 3;
S.L("ASFLGH");
//getFragmentManager().beginTransaction().add(R.id.aonboarding_fl_inputs, new WeightFragment()).commit();
break;
}
}
但是我收到这个错误:
java.lang.IllegalArgumentException: No view found for id 0x7f0f00a9
(in.jiyofit.newjiyofit:id/aonboarding_fl_inputs) for fragment TestFragment
我将 getFragmentManager 放在 postDelayed 线程中。 activity_onboarding 布局变得很好。但我仍然收到错误。 这是 TestFragment 的 xml:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="TEST"
android:gravity="center"/>
</LinearLayout>
XML 父布局
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:orientation="vertical"
android:gravity="center_horizontal"
android:layout_weight="0.15">
//some code
</LinearLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.35"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:gravity="center">
//some code
</RelativeLayout>
<FrameLayout
android:id="@+id/aonboarding_fl_inputs"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.3"/>
<Button
android:id="@+id/aonboarding_btn_proceed"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.1"
android:text="@string/submit"
style="@style/ProceedButtonStyle"/>
getFragmentManager().beginTransaction().add(R.id.aonboarding_fl_inputs, new TestFragment()).commit();
用以下几行更改上面的行。
this.getChildFragmentManager().beginTransaction().add(R.id.aonboarding_fl_inputs, new TestFragment());
t.commit();
注释:
当您从 activity 添加或替换 fragment
时,您可以直接使用您的 activity 片段管理器,即 getFragmentManager
但是当您需要添加/替换片段时从片段布局然后引用 getFragmentManager()
将使代码从父 activity 的 xml 而不是片段 xml 中查找。因此它不会在那里找到任何引用您提供的 ID 的视图,应用程序将崩溃。
您需要使用 getChildFragmentManager()
来引用当前片段。