The fragment is not displayed after bloating [Fixed, but ...]

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

Since I have no experience with Fragments , I decided to first implement the fragment in the “filler”. I created a simple snippet containing only a button and some text, and will try to inflate this in my filler activity.

However, after inflation, this fragment does not appear. LogCat prints a message stating that the fragment is bloating.

I am sure that I have something missing, but since I have no experience in this, and I could not find a textbook that explained how to do this, I do not know what it is that I am absent.

A fragment of the music player

 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> 

Fill 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(); } }); } } 

Layout Fill

 <?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 am using Android 2.3 with a compatibility library.

Any help is appreciated


The problem was fixed with the help of Yashwant-Kumar and blessings. One gave an XML solution, the other a software solution. Now I'm just wondering which solution is the most coveted. Are there any concessions in any of the solutions, or does it all come down to the programmer’s preferred solution?

+6
source share
2 answers

I have not tried adding snippets directly to xml. But what I usually do is add a frame or linear layout to the fill layout.

Suppose its id is "fragment_holder". I use the following code in fragment activity right after setContentView. try it

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

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

change what 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" /> 
+7
source

Source: https://habr.com/ru/post/902833/


All Articles