我有这个片段是为了展示一张地图。当我第二次尝试进入片段时,出现此错误:
10-09 19:40:34.050: E/AndroidRuntime(21859): FATAL EXCEPTION: main
10-09 19:40:34.050: E/AndroidRuntime(21859): java.lang.IllegalStateException: Activity has been destroyed
10-09 19:40:34.050: E/AndroidRuntime(21859): at android.support.v4.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1358)
10-09 19:40:34.050: E/AndroidRuntime(21859): at android.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java:595)
10-09 19:40:34.050: E/AndroidRuntime(21859): at android.support.v4.app.BackStackRecord.commit(BackStackRecord.java:574)
10-09 19:40:34.050: E/AndroidRuntime(21859): at com.example.cultmacedonia_fragment.map.myMapFragment2.onCreateView(myMapFragment2.java:92)
10-09 19:40:34.050: E/AndroidRuntime(21859): at android.support.v4.app.Fragment.performCreateView(Fragment.java:1478)
10-09 19:40:34.050: E/AndroidRuntime(21859): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:927)
10-09 19:40:34.050: E/AndroidRuntime(21859): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
10-09 19:40:34.050: E/AndroidRuntime(21859): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
10-09 19:40:34.050: E/AndroidRuntime(21859): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1460)
10-09 19:40:34.050: E/AndroidRuntime(21859): at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:440)
10-09 19:40:34.050: E/AndroidRuntime(21859): at android.os.Handler.handleCallback(Handler.java:615)
10-09 19:40:34.050: E/AndroidRuntime(21859): at android.os.Handler.dispatchMessage(Handler.java:92)
10-09 19:40:34.050: E/AndroidRuntime(21859): at android.os.Looper.loop(Looper.java:137)
10-09 19:40:34.050: E/AndroidRuntime(21859): at android.app.ActivityThread.main(ActivityThread.java:4898)
10-09 19:40:34.050: E/AndroidRuntime(21859): at java.lang.reflect.Method.invokeNative(Native Method)
10-09 19:40:34.050: E/AndroidRuntime(21859): at java.lang.reflect.Method.invoke(Method.java:511)
10-09 19:40:34.050: E/AndroidRuntime(21859): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
10-09 19:40:34.050: E/AndroidRuntime(21859): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
10-09 19:40:34.050: E/AndroidRuntime(21859): at dalvik.system.NativeStart.main(Native Method)
第 92 行在哪里
getChildFragmentManager().beginTransaction()
.add(R.id.map, mMapFragment).commit();
这是我的片段代码:
public class myMapFragment2 extends SherlockFragment {
private MapView mMapView;
private static GoogleMap mMap;
private Bundle mBundle;
private LocationManager locationManager;
private String provider;
static boolean isSinglePane;
private SupportMapFragment mMapFragment;
double latitude = 17.385044;
double longitude = 78.486671;
public static SherlockFragment newInstance() {
myMapFragment2 mFrgment = new myMapFragment2();
return mFrgment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
GoogleMapOptions mapOptions = new GoogleMapOptions();
Bundle bundle = getArguments();
if (bundle != null) {
isSinglePane = bundle.containsKey("KEY_DETAIL");
} else {
isSinglePane = false;
}
mapOptions.compassEnabled(true)
.camera(new CameraPosition(new LatLng(latitude, longitude), 13,
0f, 0f));
mMapFragment = SupportMapFragment.newInstance(mapOptions);
Toast.makeText(getActivity().getApplicationContext(),
String.valueOf(isSinglePane), 1000).show();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View inflatedView = inflater.inflate(R.layout.maps3, container, false);
setHasOptionsMenu(true);
try {
MapsInitializer.initialize(getActivity());
} catch (GooglePlayServicesNotAvailableException e) {
// TODO handle this situation
Log.i("***GooglePlayServicesNotAvailableException", e.toString());
}
getChildFragmentManager().beginTransaction()
.add(R.id.map, mMapFragment).commit();
setUpMapIfNeeded(inflatedView);
return inflatedView;
}
private void setUpMapIfNeeded(View inflatedView) {
if (mMap == null) {
mMap = ((MapView) inflatedView.findViewById(R.id.map)).getMap();
if (mMap != null) {
setUpMap();
}
}
}
private void setUpMap() {
Marker marker = mMap.addMarker(new MarkerOptions()
.position(new LatLng(latitude, longitude))
.title("You are here")
.snippet("your point")
.icon(BitmapDescriptorFactory
.defaultMarker(BitmapDescriptorFactory.HUE_AZURE)));
// create marker
marker.showInfoWindow();
// color icon
// marker.icon(BitmapDescriptorFactory.fromResource(R.drawable.my_marker_icon)));
// adding marker
mMap.setMyLocationEnabled(true);
mMap.getUiSettings().setCompassEnabled(true);
locationManager = (LocationManager) getActivity()
.getApplicationContext().getSystemService(
Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
provider = locationManager.getBestProvider(criteria, false);
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.map_menu, menu);
super.onCreateOptionsMenu(menu, inflater);
}
public static void satelliteMap() {
mMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
}
public static void normalMap() {
mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
}
public static void menuAll() {
}
public static void menuNearMe() {
}
@Override
public void onResume() {
super.onResume();
if (mMap == null) {
mMap = mMapFragment.getMap();
if (mMap != null) {
setUpMap();
}
}
}
@Override
public void onPause() {
super.onPause();
getChildFragmentManager().beginTransaction().remove(mMapFragment)
.commit();
}
public void onDestroyView() {
super.onDestroyView();
FragmentTransaction ft = getActivity().getSupportFragmentManager()
.beginTransaction();
ft.remove(this);
ft.commit();
}
}
和
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<com.google.android.gms.maps.MapView
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
第一次一切正常,但第二次应用程序崩溃。