I want to include maps in my android app so I followed the official docs and ended up with these files
fragment_map.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="This is the map fragment"/>
<fragment
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/map"
tools:context="com.example.nirmal.attendancetracker"
class="com.google.android.gms.maps.SupportMapFragment"
/>
</LinearLayout>
MapsFragment.java:
public class MapFragment extends Fragment implements OnMapReadyCallback{
SupportMapFragment mapFragment;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View subView = inflater.inflate(R.layout.fragment_map,container,false);
mapFragment = (SupportMapFragment)getActivity().getSupportFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
return subView;
}
@Override
public void onMapReady(GoogleMap googleMap) {
LatLng sydney = new LatLng(-33.852, 151.211);
googleMap.addMarker(new MarkerOptions().position(sydney)
.title("Marker in Sydney"));
googleMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
}
}
As you may have seen from my code. I am trying to show the map in a fragment which in turn is shown as a part of a ViewPager
. I did everything as per the documentatio but the app is crashing with the following error when I run it
FATAL EXCEPTION: main Process: com.example.nirmal.attendancetracker, PID: 31903
android.view.InflateException: Binary XML file line #13: Error inflating class fragment
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:713)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
at com.example.nirmal.attendancetracker.MapFragment.onCreateView(MapFragment.java:27)
I have seen these errors before. They are usually caused by wrong XML statements. Since I am new to this maps thing I am unable to find any errors in that.
Is XML file the issue? or am I doing something else wrong?