我有一个片段,以标签布局显示。
public class MyFragment extends Fragment {
private GoogleMap map;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_map, container, false);
map = ((SupportMapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
...
然而,当再次调用onCreateView时遇到问题,inflate方法失败,因为已经存在MapFragment。
我不确定这里的正确答案是什么,因为我觉得这是android中的一个错误。 I.E.我没有以编程方式创建SupportMapFragment,所以我觉得我不应该以编程方式销毁它。
我看到'解决方案'表示你需要销毁它:
public void onDestroy() {
try {
Fragment fragment = (getFragmentManager().findFragmentById(R.id.map_fragment));
FragmentTransaction ft = getActivity().getSupportFragmentManager().beginTransaction();
ft.remove(fragment);
ft.commit();
} catch (Exception e) {
e.printStackTrace();
}
}
但是,如果我这样做,我会得到:
> 04-07 13:59:00.801: W/System.err(20067):
> java.lang.IllegalStateException: Can not perform this action after
> onSaveInstanceState 04-07 13:59:00.801: W/System.err(20067): at
> android.support.v4.app.FragmentManagerImpl.checkStateLoss(FragmentManager.java:1343)
> 04-07 13:59:00.801: W/System.err(20067): at
> android.support.v4.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1361)
> 04-07 13:59:00.801: W/System.err(20067): at
> android.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java:595)
这是我的片段XML:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.google.android.gms.maps.SupportMapFragment"
map:uiCompass="false"
map:uiRotateGestures="false"
map:uiTiltGestures="false"/>
</RelativeLayout>
如果Android膨胀了SupportMapFragment,它也不会破坏该片段。如果不是我应该如何正确地销毁SupportMapFragment,以便对onCreateView的另一次调用不会失败,而且我也不会在onDestroyView()中得到异常。
答案 0 :(得分:-1)
我尝试使用带有不同类型片段的supportMapFragment或MapFragment,它总是让我很难,所以对我来说唯一正常的解决方案是手动实现它,这实际上是几行代码。
这是MapActivity
public class Map extends SherlockFragment implements OnMapClickListener {
public static GoogleMap googleMap;
private MapView mMapView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.map);
mMapView = (MapView) findViewById(R.id.mapview);
mMapView.onCreate(savedInstanceState);
initilizeMap();
}
/**
* function to load map. If map is not created it will create it for you
* */
private void initilizeMap() {
googleMap = null;
googleMap = ((MapView) findViewById(R.id.mapview)).getMap();
if (googleMap != null) {
googleMap.setMapType(googleMap.MAP_TYPE_NORMAL);
googleMap.setOnMapClickListener(this);
googleMap.setMyLocationEnabled(true);
googleMap.setLocationSource(new CurrentLocationProvider(this));
}
}
@Override
public void onResume() {
super.onResume();
mMapView.onResume();
Log.e("onResume","onResume");
}
@Override
public void onPause() {
super.onPause();
mMapView.onPause();
Log.e("onPause","onPause");
}
@Override
public void onDestroy() {
if(mMapView!=null){
mMapView.onDestroy();
Log.e("map view not null !!","it is not null");
}else{
Log.e("map view null!!","it is null");
}
super.onDestroy();
}
@Override
public void onMapClick(LatLng latng) {
}
}
CurrentLocationProvider:
public class CurrentLocationProvider implements LocationSource, LocationListener
{
private OnLocationChangedListener listener;
private LocationManager locationManager;
public CurrentLocationProvider(Context context)
{
locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
}
@Override
public void activate(OnLocationChangedListener listener)
{
this.listener = listener;
LocationProvider gpsProvider = locationManager.getProvider(LocationManager.GPS_PROVIDER);
if(gpsProvider != null)
{
locationManager.requestLocationUpdates(gpsProvider.getName(), 0, 10, this);
}
LocationProvider networkProvider = locationManager.getProvider(LocationManager.NETWORK_PROVIDER);;
if(networkProvider != null) {
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1000 * 60 * 5, 0, this);
}
}
@Override
public void deactivate()
{
locationManager.removeUpdates(this);
}
@Override
public void onLocationChanged(Location location)
{
if(listener != null)
{
listener.onLocationChanged(location);
}
}
@Override
public void onProviderDisabled(String provider)
{
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String provider)
{
// TODO Auto-generated method stub
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras)
{
// TODO Auto-generated method stub
}
}
这是布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<com.google.android.gms.maps.MapView
android:id="@+id/mapview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.75" />
</LinearLayout>