Frage

Good morning. I'm developing an app in which I have a main layout that extends from activity and inside this one I have one fragment of one data type, in my case FragmentCover (it's a class).

During my app, I push a button and I want to change this fragment layout for another layout that extends from fragment but of different type, called SongList.

My problem is that I have defined this fragment for the class of Cover and when I change I don't have any problem, but when I want to get the views and set to one variable of my class, the funciont songList = (ListView) getView().findViewById(R.id.songList); it's null and it gives me error.

I put it here what I do.

layout

<fragment android:name="es.xxx.ui.FragmentCover"
    android:id="@+id/pruebaa"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_above="@id/songBar"
    android:layout_below="@id/header"/>

mainclass change

public void onClick(View v) {
if(isCoverFragment){
                FragmentSongList fragmentSongList = new FragmentSongList();
                transaction = getFragmentManager().beginTransaction();
                transaction.replace(R.id.pruebaa, fragmentSongList);
                transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
                transaction.addToBackStack("LIST");
                transaction.commit();
                isCoverFragment=false;
            }
            else{
                transaction = getFragmentManager().beginTransaction();
                transaction.replace(R.id.pruebaa, fragmentCover);
                transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
                transaction.addToBackStack("COVER");
                transaction.commit();
                getSongCover();
                isCoverFragment=true;
            }

The problem is not the change, is when I try to make the findViewById, it's probabbly because it doesn't load the view associated with FragmentSongList.

War es hilfreich?

Lösung

You need to use a FrameLayout inside your layout file, instead of defining the Fragment in .xml

Into that FrameLayout, you can inflate any Fragment you want. Just inflate the First Fragment you want to be displayed directly in your onCreate(...) method.

<FrameLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/content_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent">       
        </FrameLayout>

This is how to inflate the Fragments programatically into the FrameLayout.

 FragmentSongList fragmentSongList = new FragmentSongList();
 FragmentTransaction transaction = getFragmentManager().beginTransaction();
         transaction.replace(R.id.content_frame, fragmentSongList);
         transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
         transaction.addToBackStack("LIST");
         transaction.commit();

You can leave the code inside your onClick() method just the way it is, just change the id to "content_frame". Furthermore, as mentioned above you will have to inflate the first Fragment that should be displayed inside your onCreate(...) method.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top