1

I can't seem to be able to add google map inside a fragment. Here's my code

activity_main.xml

<RelativeLayout 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"
    tools:context="com.example.test.MainActivity">
    <fragment
        android:id="@+id/fragment1"
        android:name="com.example.test.MyMapFragment"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
</RelativeLayout>

fragment_mymap.xml

<RelativeLayout 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"
    tools:context=".MainActivity" >
    <fragment
        android:id="@+id/google_map"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        class="com.google.android.gms.maps.SupportMapFragment" />
</RelativeLayout>

MyMapFragment.java

public class MyMapFragment extends Fragment implements OnMapReadyCallback
{
    private GoogleMap mMap;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_mymap, container, false);

        SupportMapFragment mapFragment = (SupportMapFragment) getFragmentManager().findFragmentById(R.id.google_map);
        mapFragment.getMapAsync(this);

        // Inflate the layout for this fragment
        return v;
    }

    @Override
    public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;
    }
}

Yet, I get error of android.view.InflateException: Binary XML file line #8: Error inflating class fragment.

I have tried using MapView in fragment as like in this post, or this one. But the thing is, mapView.getMap() is now deprecated. Though the program threw no error, the map is displayed but it's blank (Just showing Google logo on bottom left corner).

Any suggestion is appreciated.

Community
  • 1
  • 1
Dicky Bullin
  • 259
  • 1
  • 2
  • 10
  • change this line class="com.google.android.gms.maps.SupportMapFragment" to app:class="com.google.android.gms.maps.SupportMapFragment" and add xmlns:app="http://schemas.android.com/apk/res-auto". hope this help – Shubham AgaRwal Mar 30 '16 at 19:46
  • @Shubham It still doesn't work. The editor underlines the `app:class` with error message of `Unexpected namespace prefix "app" found for tag fragment`. Any other suggestion? – Dicky Bullin Mar 30 '16 at 19:51
  • yes use namespace xmlns:app="http://schemas.android.com/apk/res-auto" in the fragment or in its parent – Shubham AgaRwal Mar 30 '16 at 20:01
  • @Shubham I've tried both. If I add it to its parent, it will underline the `app:class`. If I add it to the fragment, it will still underline and throw an error of `android.view.InflateException: Binary XML file line #8: Error inflating class fragment` – Dicky Bullin Mar 30 '16 at 20:05
  • @d.datul1990 I think I didn't happen to nest the google map fragment into another fragment.. I went for another approach.. – Dicky Bullin Oct 18 '16 at 03:39

1 Answers1

0

Yes, 'mapView.getMap()' is now deprecated. Use 'getMapAsync' the object that will be triggered when the map is ready to be used.

For the error 'Error inflating class fragment.' please check the project build target and minimum target sdk version. Also, don't forget to include your maps permission.

I found this related SO ticket which is related to your issue. There are some solutions offered by other OP and it might solve your issue.

You can also use the Official Google Documentation for Maps Android API, this docs is a quick start on how to create Google maps to an Android app: https://developers.google.com/maps/documentation/android-api/start

Community
  • 1
  • 1
Android Enthusiast
  • 4,826
  • 2
  • 15
  • 30