9

나는지도 활동 (SherlockFragmentActivity)가 그 2 개 개의 다른 조각을 보여주는 사이에 2 개 버튼과 스위치가있는지도 조각에서 부하 (GoogleMapFragment & ScaleImageViewFragment)전환 조각 - 구글지도 조각 V2 - 활동 파괴 된 오류

오류

ERROR/AndroidRuntime(5768): FATAL EXCEPTION: main 
    java.lang.IllegalStateException: Activity has been destroyed 
    at android.support.v4.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1342) 
    at android.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java:595) 
    at android.support.v4.app.BackStackRecord.commit(BackStackRecord.java:574) 
    at com.takeit2eleven.towniemeeting.android.fragments.GoogleMapFragment.onCreateView(GoogleMapFragment.java:42) 
    at android.support.v4.app.Fragment.performCreateView(Fragment.java:1460) 
    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:911) 
    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1088) 
    at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682) 
    at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1444) 
    at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:429) 
    at android.os.Handler.handleCallback(Handler.java:615) 
    at android.os.Handler.dispatchMessage(Handler.java:92) 
    at android.os.Looper.loop(Looper.java:137) 
    at android.app.ActivityThread.main(ActivityThread.java:4745) 
    at java.lang.reflect.Method.invokeNative(Native Method) 
    at java.lang.reflect.Method.invoke(Method.java:511) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 
    at dalvik.system.NativeStart.main(Native Method) 
에게 "활동 파괴 된 java.lang.IllegalStateException"나는 조각을 대체 전시지도 버튼을 클릭하면 내가 다시 클릭하면 Screen Shot

그러나 나는 얻을

public class MapsFragment extends SherlockFragment { 

Button mHotelMapButton; 
Button mExhibitorButton; 
GoogleMapFragment mGoogleMapFragment; 
ScaleImageViewFragment mScaleImageViewFragment; 
static boolean mShowingGoogleMap = true; 


@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    if (savedInstanceState != null) { 
     mShowingGoogleMap = savedInstanceState.getBoolean("showingGoogleMap"); 
    } 

    mGoogleMapFragment = new GoogleMapFragment(); 
    mScaleImageViewFragment = new ScaleImageViewFragment(); 

} 

@Override 
public void onSaveInstanceState(Bundle outState) { 
    super.onSaveInstanceState(outState); 
    outState.putBoolean("showingGoogleMap", mShowingGoogleMap); 

} 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 

    View view = inflater.inflate(R.layout.maps_fragment, null); 
    mHotelMapButton = (Button) view.findViewById(R.id.hotel_button); 
    mExhibitorButton = (Button) view.findViewById(R.id.exhititor_map_button); 

    mExhibitorButton.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      mShowingGoogleMap = false; 
      showExhibitorFragment(); 
     } 
    }); 

    mHotelMapButton.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      mShowingGoogleMap = true; 
      showGoogleMapFragment(); 
     } 
    }); 

    return view; 

} 

@Override 
public void onPause() { 
    super.onPause(); 
    getChildFragmentManager().beginTransaction() 
      .remove(mGoogleMapFragment) 
      .remove(mScaleImageViewFragment).commit(); 
} 

@Override 
public void onResume() { 
    super.onResume(); 


    if(mShowingGoogleMap){ 
     showGoogleMapFragment(); 

    }else{ 
     showExhibitorFragment(); 
    } 
} 

private void showExhibitorFragment(){ 
    getChildFragmentManager().beginTransaction() 
      .remove(mGoogleMapFragment) 
      .remove(mScaleImageViewFragment) 
      .replace(R.id.fragment_placeholder, mScaleImageViewFragment, "scaleImage") 
      .commit(); 
} 
private void showGoogleMapFragment(){ 
    getChildFragmentManager().beginTransaction() 
      .remove(mGoogleMapFragment) 
      .remove(mScaleImageViewFragment) 
      .replace(R.id.fragment_placeholder, mGoogleMapFragment, "mapFragment") 
      .commit(); 
} 
} 

내 GoogleMapFragment.java 파일 : 617,451,515,

여기 내 MapsFragment.java의 파일이 오류를 알아 내려고 꽤 많은 시간 동안 벽에 머리를 두드리는 된

public class GoogleMapFragment extends Fragment { 
private SupportMapFragment mMapFragment; 
private GoogleMap mMap; 
private final LatLng PLANET_HOLLYWOOD = new LatLng(36.1100, -115.1710); 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    GoogleMapOptions mapOptions = new GoogleMapOptions(); 
    mapOptions.compassEnabled(true).camera(new CameraPosition(PLANET_HOLLYWOOD, 13, 0f, 0f)); 
    mMapFragment = SupportMapFragment.newInstance(mapOptions); 
} 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    View view = inflater.inflate(R.layout.google_map, container, false); 

    getChildFragmentManager() 
      .beginTransaction() 
      .add(R.id.map_holder, mMapFragment) 
      .commit(); 

    return view; 

} 

@Override 
public void onPause() { 
    super.onPause(); 
    getChildFragmentManager().beginTransaction() 
      .remove(mMapFragment) 
      .commit(); 
} 


@Override 
public void onResume() { 
    super.onResume(); 

    mMap = mMapFragment.getMap(); 
    if(mMap != null){ 
     mMap.addMarker(new MarkerOptions().position(PLANET_HOLLYWOOD).title("Planet Hollywood")); 
    } 
} 
} 

그것을 피하고/고치는 법.

업데이트 : 그런데

내가로 전환하면 다음 GoogleMapFragment에

getActivity().getSupportFragmentManager() 
      .beginTransaction() 
      .add(R.id.map_holder, mMapFragment) 
      .commit(); 

는 내가 오류를 얻을 해달라고,하지만 난 메이커를 설정하는 맵의 잡아 질수 .. . 그것은 경쟁 조건처럼 보인다

답변

13

당신이 onDestroyView()의 모든 중첩 된 조각을 삭제하면,이 문제를 해결할 수 있습니다

public void onDestroyView() 
{ 
     super.onDestroyView(); 
     Fragment fragment = (getFragmentManager().findFragmentById(R.id.map)); 
     FragmentTransaction ft = getActivity().getSupportFragmentManager().beginTransaction(); 
     ft.remove(fragment); 
     ft.commit(); 
} 

그리고 onCreateView()에서 평소을 팽창 :

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
    return inflater.inflate(R.layout.map, container, false); 
} 
+0

그냥 우수한! API 16을 사용하고자하는 사용자는 NestedFragments를 사용할 수 없으므로이 솔루션은 제 경우에는 정상적으로 작동합니다! – Otuyh