Reputation: 747
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?
Upvotes: 0
Views: 635
Reputation: 21756
As you are using SupportMapFragment
from Fragment
its best prectice ot Use com.google.android.gms.maps.MapView
instead of SupportMapFragment
<com.google.android.gms.maps.MapView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/map" />
Your MapFragment class as below:
public class MapFragment extends Fragment {
MapView mapView;
GoogleMap map;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View subView = inflater.inflate(R.layout.fragment_map, container, false);
mapView = (MapView) subView.findViewById(R.id.map);
mapView.onCreate(savedInstanceState);
map = mapView.getMap();
map.getUiSettings().setMyLocationButtonEnabled(false);
map.setMyLocationEnabled(true);
try {
MapsInitializer.initialize(this.getActivity());
} catch (GooglePlayServicesNotAvailableException e) {
e.printStackTrace();
}
CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(new LatLng(13.1, -37.9), 10);
map.animateCamera(cameraUpdate);
return subView;
}
@Override
public void onResume() {
mapView.onResume();
super.onResume();
}
@Override
public void onPause() {
super.onPause();
mapView.onPause();
}
@Override
public void onDestroy() {
super.onDestroy();
mapView.onDestroy();
}
}
Hope this will help~
Upvotes: 1
Reputation: 191844
Looks like you have confused the official documentation and have attempted to place a Fragment within a Fragment.
I also see you are not inflating your MapFragment
class... So, the error seems to exist elsewhere.
class="com.google.android.gms.maps.SupportMapFragment"
Quoting the documentation...
By default, the XML file that defines the app's layout is at
res/layout/activity_maps.xml
. It contains the following 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" />
The maps activity Java file
By default, the Java file that defines the maps activity is named
MapsActivity.java
. It should contain the following code after your package name:public class MapsActivity extends FragmentActivity implements OnMapReadyCallback { private GoogleMap mMap; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_maps); SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager() .findFragmentById(R.id.map); mapFragment.getMapAsync(this); } @Override public void onMapReady(GoogleMap googleMap) { mMap = googleMap;
So, all that code shown in your question should really be in an Activity, not a Fragment, and then replace
getActivity().getSupportFragmentManager().findFragmentById(R.id.map)
With simply
getSupportFragmentManager().findFragmentById(R.id.map)
If you really want to extend the SupportMapFragment
class, start with this
public class MapFragment extends SupportMapFragment
implements OnMapReadyCallback {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getMapAsync(this);
}
And load that in the XML
<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="YOUR.PACKAGE.NAME.MapFragment" /> <!--- See here --->
Upvotes: 0
Reputation: 720
I had the same issue! Try adding android:name="com.testing.shivam.MainActivity" to "fragment" in the layout! It solved the issue for me
<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.SupportMapFragment"
android:name="com.testing.shivam.MainActivity"/>
If this doesn't work for you then check whether you have properly added meta tag for google_play_services_version in your manifest file under application tag,
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
Upvotes: 0