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.