Developer2012
Developer2012

Reputation: 169

Error inflating class fragment

I`m trying to inflate a map fragment inside a fragment like the code below:

public class HomeFragment extends Fragment {

    public HomeFragment(){}
    public GoogleMap mMap;
    private LocalizeDB db;
    private FragmentActivity myContext;


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

        View rootView = inflater.inflate(R.layout.activity_maps, container, false);

        return rootView;
    }

    @Override
    public void onResume() {
        super.onResume();
        setUpMapIfNeeded();
    }
}

the activity_maps.xml code

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/map"
tools:context=".MapsActivity"
android:name="com.google.android.gms.maps.SupportMapFragment"/>

I'm getting the following error: Binary XML file line #1: Error inflating class fragment

If instead of inflate activity_maps.xml, I inflate fragment_home.xml, it works. But I need to inflate the map fragment... (activity_maps.xml)

fragment_home.xml code:

    <?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/txtLabel"
         android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:textSize="16dp"
        android:text="Home View"/>

    <ImageView android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/txtLabel"
        android:src="@drawable/ic_home"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="10dp"/>

</RelativeLayout>

Upvotes: 0

Views: 291

Answers (3)

dora
dora

Reputation: 2077

We cannot use static MapFragments if you inflating that layout in a fragment. Instead you can use a FrameLayout inside the xml and in the java class you should replace that container with the Map fragment dynamically.

For eg:

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

and in java file you should replace that fragment container like below:

 @Override
public View onCreateView(LayoutInflater inflater,
        ViewGroup container, Bundle    savedInstanceState) {
    return inflater.inflate(R.layout.activity_maps, container, false);
}

   @Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    FragmentManager fm = getFragmentManager();
    fm.beginTransaction().replace(R.id.map, new SupportMapFragment()).commit();

}

i think that should help. Feel free to ask any clarification if needed.

Upvotes: 1

Developer2012
Developer2012

Reputation: 169

I missed the google maps API KEY in AndroidManifest.xml

Upvotes: 0

Fahim
Fahim

Reputation: 12378

Change

android:name="com.google.android.gms.maps.SupportMapFragment"

To

class="com.google.android.gms.maps.SupportMapFragment

"

Upvotes: 1

Related Questions