1

I'm trying to display GoogleMaps inside a FrameLayout. This FrameLayout is supposed to move up and down according to a toggle button. However, my app crashes when I try to inflate the View.

Is it even possible what I'm doing here? I mean displaying GoogleMaps inside a FrameLayout? How can I make this work?

Thank you!

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <RelativeLayout
        android:id="@+id/fragment_a"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <TextView
            android:id="@+id/lv_container"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="bottom"
            android:text="Hello World!"/>
    </RelativeLayout>
    <!-- this frame is invisible -->
    <FrameLayout
        android:id="@+id/fragment_b"
        android:visibility="invisible"
        android:layout_width="fill_parent"
        android:layout_height="200dp"
        android:background="#FF0000">
        <fragment
            android:id="@+id/map_v2"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            class="com.google.android.gms.maps.SupportMapFragment" />
    </FrameLayout>
</LinearLayout>

The according code inside my Fragment class:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    final View rootView            = inflater.inflate(R.layout.fragment_favorites, container, false);
    final FragmentManager fm       = getFragmentManager();
    final RelativeLayout fragmentA = (RelativeLayout) rootView.findViewById(R.id.fragment_a);

    fragmentA.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            RelativeLayout fragmentA = (RelativeLayout) getActivity().findViewById(R.id.fragment_a);
            FrameLayout fragmentB    = (FrameLayout)getActivity().findViewById(R.id.fragment_b);

            int y = 200;
            if (state == true) {
                if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
                    fragmentA.animate().translationY(-y).setDuration(500);
                    fragmentB.animate().translationY(-y).setDuration(500);
                    state = false;
                }
            } else {
                if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
                    fragmentA.animate().translationY(y).setDuration(500);
                    fragmentB.animate().translationY(y).setDuration(500);
                    state = true;
                }
            }
        }
    });
    return rootView;
}

The error I'm getting:

04-19 15:39:43.248    8758-8758/com.mahlzeit E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.mahlzeit, PID: 8758
    android.view.InflateException: Binary XML file line #20: Error inflating class fragment
            at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:719)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:761)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:498)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:398)
            at com.mahlzeit.mahlzeitactivity.FavoritesFragment.onCreateView(FavoritesFragment.java:27)
            at android.support.v4.app.Fragment.performCreateView(Fragment.java:1786)
            at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:953)
Stefan Falk
  • 23,898
  • 50
  • 191
  • 378

1 Answers1

1

Looks like you're nesting Fragments. You can't have < fragment /> tags in a Fragment layout.

Check here: Fragments within Fragments

Add the fragment dynamically with getChildFragmentManager() inside your Fragment.

Community
  • 1
  • 1
razzledazzle
  • 6,900
  • 4
  • 26
  • 37
  • @AhmadAlsanie you don't agree? Is there another way? – Stefan Falk Apr 19 '15 at 14:11
  • Say you have a layout fragment_about.xml and inside it you included a < fragment /> tag, the Fragment will fail to inflate. You should programatically add a Fragment (beginTransaction(), replace(), etc.) with a FragmentManager inside the containing Fragment. – razzledazzle Apr 19 '15 at 14:13
  • @razzledazzle So I'd have another class `MyGoogleMapsFragment extends Fragment` which I add like you said? – Stefan Falk Apr 19 '15 at 14:15
  • @StefanFalk inside your FavoritesFragment (based on the stack trace) or any containing Fragment, use getChildFragmentManager and beginTransaction to replace a ViewGroup (for example a FrameLayout) inside the Fragment layout. You don't need to extend it. In your current case, ` ` – razzledazzle Apr 19 '15 at 14:42