سؤال

I'm trying to create a music player that will be visible in all my activities. To do this, I'm going to use a Fragment as some of you advised me earlier.

Since I have no experience with Fragments whatsoever, I decided to implement a fragment in a "filler" activity first. I created a simple fragment containing only a button and some text, and try to inflate that in my filler activity.

However, after inflating this fragment does not show up. A message does get printed to LogCat telling me that the fragment has been inflating.

I'm pretty sure I'm missing something, but since I have no experience with this and I couldn't find a tutorial that quite explained how to do this, I don't know what it is that I'm missing.

Music player Fragment

public class AppMusicPlayer extends Fragment{


     @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            // Inflate the layout for this fragment
         System.out.println("fragment added");
            return inflater.inflate(R.layout.musicplayer_main, container, false);
        }


}

Music Player layout

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >


    <Button
        android:id="@+id/button1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Button" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="This is the fragment you're looking for" />

</LinearLayout>

Filler Activity

public class Filler extends FragmentActivity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.filler);

        Button b = (Button) findViewById(R.id.terug);
        b.setOnClickListener(new View.OnClickListener() {
            public void onClick(View arg0) {
                Intent intent = new Intent();
                setResult(RESULT_OK, intent);
                finish();
            }
        });

    }

}

Filler layout

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
<!-- Title bar -->
    <RelativeLayout
        android:id="@+id/linearLayout1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:adjustViewBounds="true"
        android:background="@drawable/title_bar" >

        <TextView
            android:id="@+id/fillertitle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:text="Work In Progress"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:textStyle="bold" >
        </TextView>

        <Button
            android:id="@+id/terug"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:layout_centerVertical="true"
            android:background="@drawable/button_back"
            android:text="Back"
            android:textColor="#FFFFFFFF"
            android:textStyle="bold" >
        </Button>
    </RelativeLayout>
<!-- End of title bar -->
    <TextView
        android:id="@+id/wip"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="This section of the app has not been made yet. Work in progress." >
    </TextView>

    <fragment
        android:id="@+id/musicPlayerFragment"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:name="com.mobowski.app.player.AppMusicPlayer" />

</LinearLayout>

Obviously I'm doing something wrong, but what?

Note: I'm using Android 2.3 with the Compatibility library.

Any help is appreciated


The problem has been fixed with the help of Yashwanth Kumar and blessenm. One gave an XML solution, the other a programmatic solution. Now I'm just wondering which solution is the most desirable one. Are there any concessions to be made with either of the solutions, or does it all boil down to the programmer's preferred solution?

هل كانت مفيدة؟

المحلول

I haven't tried adding fragments directly to xml. But what I normally do is add a framelayout or linearlayout to the filler layout.

Suppose its id is 'fragment_holder'. I use the following code in the fragment activity right after setContentView. Try this

FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.add(R.id.fragment_holder,new AppMusicPlayer(),"musicplayer");
ft.commit();

نصائح أخرى

you should mention layout_height as 0dip, not the width in vertical linear layout.

change that it should work.

<fragment
        android:id="@+id/musicPlayerFragment"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:name="com.mobowski.app.player.AppMusicPlayer" />
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top