2017-01-09 113 views
-1

我有一個需要基於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> 

父佈局

<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"/> 
+0

發佈dialogfragment的xml .. – rafsanahmad007

+0

在activity_onboarding佈局中,您是否有任何帶有id aonboarding_fl_inputs的框架或其他佈局? –

+0

是的,我確實有 – suku

回答

3
getFragmentManager().beginTransaction().add(R.id.aonboarding_fl_inputs, new TestFragment()).commit(); 

與以下行更改上述各行的XML。

this.getChildFragmentManager().beginTransaction().add(R.id.aonboarding_fl_inputs, new TestFragment()); 
    t.commit(); 

注:

當U添加或從活動取代fragment則可以直接使用u您的活動片段經理即getFragmentManager但是當u需要添加/替換的片段佈局片段,然後指getFragmentManager()將使代碼從您的父級活動的xml中找到,而不是片段xml。所以它不會找到任何提及您提供的id的視圖,並且應用會崩潰。

您需要使用getChildFragmentManager()來引用當前片段。

+0

'getFragmentManager'和'getChildFragmentManager'有什麼區別 – suku

+1

@suku:引用更新信息 – Beena