我想在我的应用程序使用新的Android地图。
我有哪些布局包含(除其他事项外)一个FragmentActivity:
<LinearLayout a:id="@+id/fragment_container"
a:layout_width="fill_parent"
a:layout_height="100dp"
a:layout_weight="1"/>
它也有一个按钮,用于改变使用该片段(主要来自样品项目复制):
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();
和LocationFragment是:
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布局:
<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"/>
问题是,当我们与LocationFragment替换片段的第二次Android是无法从XML膨胀。 它崩溃上线:
view = inflater.inflate(R.layout.map_fragment, container, false);
有例外:
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)
...
我不知道如何解决它。 我认为,这可能与一个老问题,你不能使用一个以上的地图视图(在Android地图V1发现的问题)来连接。 但在v2和片段箱支撑它必须与地图片段取代片段容器的方式,对吗?