Issue
I'm trying to use new android maps in my app.
I've got a FragmentActivity which layout contains (among other things):
<LinearLayout a:id="@+id/fragment_container"
a:layout_width="fill_parent"
a:layout_height="100dp"
a:layout_weight="1"/>
It also has a button which changes this fragment using (mostly copied from sample project):
if (condition) {
fragment = new Fragment1();
} else {
fragment = new LocationFragment();
}
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.fragment_container, fragment);
fragmentTransaction.commit();
and LocationFragment is:
public class LocationFragment extends SupportMapFragment{
private GoogleMap mMap;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
view = inflater.inflate(R.layout.map_fragment, container, false);
setUpMapIfNeeded();
return view;
}
@Override
public void onResume() {
super.onResume();
setUpMapIfNeeded();
}
private void setUpMapIfNeeded() {
// Do a null check to confirm that we have not already instantiated the map.
if (mMap == null) {
// Try to obtain the map from the SupportMapFragment.
mMap = ((SupportMapFragment) getActivity().getSupportFragmentManager().findFragmentById(R.id.map))
.getMap();
// Check if we were successful in obtaining the map.
if (mMap != null) {
setUpMap();
}
}
}
private void setUpMap() {
mMap.addMarker(new MarkerOptions().position(new LatLng(60.02532, 30.370552)).title(getString(R.string.map_current_location)));
}
}
xml layout is:
<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"/>
The problem is that when we replacing fragment with LocationFragment for the second time android is unable to inflate from xml. It crashes on line:
view = inflater.inflate(R.layout.map_fragment, container, false);
with exception:
12-17 01:47:31.209: ERROR/AndroidRuntime(4679): FATAL EXCEPTION: main
android.view.InflateException: Binary XML file line #4: Error inflating class fragment
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:587)
at android.view.LayoutInflater.inflate(LayoutInflater.java:386)
at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
at my.package.LocationFragment.onCreateView(LocationFragment.java:29)
...
And I have no Idea how to solve it. I think that it could be connected with an old issue that you cannot use more than one map view (found questions on android map v1). But in case of v2 and fragments support it must be the way to replace fragment container with map fragment, am I right?
Solution
You are extending SupportMapFragment
. That is perfectly fine, though I am uncertain how common that pattern will be (this is all a bit too new).
However, SupportMapFragment
is a fragment and knows how to display a map. You should not be overriding onCreateView()
in most cases, and you certainly should not be overriding onCreateView()
and trying to inflate a layout file that contains a <fragment>
element pointing back to SupportMapFragment
.
Here is a sample app that uses SupportMapFragment
from the Maps V2 library. The activity loads a layout that has SupportMapFragment
:
<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"/>
Answered By - CommonsWare
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.