对话框中的IllegalArgumentException片段

时间:2014-11-03 14:57:39

标签: java android android-fragments

我有片段问题。 Fragment中有一个Dialog,这个Dialog中有一个Fragment。 但是当我添加第二个片段时,有一个IllegalArgumentException : No view found for id ***** (fr.*****:id/container_list_collections) for fragment FragmentCollectionVignette

FragmentHP.java

public class FragmentHP extends Fragment {

/** Bouton Ajouter à **/
private Button btAddTo;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, final Bundle savedInstanceState) {
    View vueFragmentHP = inflater.inflate(R.layout.fragment_hp, container, false);

    getFragmentManager().beginTransaction().replace(R.id.container_dernier_ajout, new FragmentDerniersAjoutsHP()).commit();
    getFragmentManager().beginTransaction().replace(R.id.container_collections, new FragmentCollectionsHP()).commit();
    getFragmentManager().beginTransaction().replace(R.id.container_ebooks, new FragmentEbooksHP()).commit();
    getFragmentManager().beginTransaction().replace(R.id.container_games, new FragmentGamesHP()).commit();
    getFragmentManager().beginTransaction().replace(R.id.container_software, new FragmentSoftwareHP()).commit();
    getFragmentManager().beginTransaction().replace(R.id.container_digital_creation, new FragmentDigitalCreationHP()).commit();

    btAddTo = (Button) vueFragmentHP.findViewById(R.id.bt_collections);

    btAddTo.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            LayoutInflater inflater = getActivity().getLayoutInflater();
            View view = inflater.inflate(R.layout.dialog_add_to, null);
            final Dialog dialog = new Dialog(getActivity());

            dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
            dialog.setContentView(view);
            dialog.show();
            dialog.setCanceledOnTouchOutside(false);

            Button btValider = (Button) view.findViewById(R.id.bt_add_to);

            getFragmentManager().beginTransaction().add(R.id.container_list_collections, new FragmentCollectionVignette()).commit();
        }
    });

    return vueFragmentHP;
}

}

dialog_add_to.xml

    <?xml version="1.0" encoding="UTF-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
    <TextView 
        android:id="@+id/tv_add_to"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/add_to"/>
    <TextView 
        android:id="@+id/tv_choose_collection"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/choose_collection"
        android:layout_below="@id/tv_add_to"/>

    <FrameLayout 
        android:id="@+id/container_list_collections"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/tv_choose_collection"/>
    <Button 
        android:id="@+id/bt_add_to"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/valider"
        android:layout_below="@id/container_list_collections"/>
</RelativeLayout>

有什么问题? (抱歉我的英语非常糟糕)

1 个答案:

答案 0 :(得分:0)

我认为问题出在这一行 getFragmentManager()。beginTransaction()。add(R.id.container_list_collections,new FragmentCollectionVignette())。commit();

将此更改为 this.getActivity()。getFragmentManager()。beginTransaction()。add(R.id.container_list_collections,new FragmentCollectionVignette())。commit();