关闭应用程序时应用程序崩溃

时间:2014-10-24 03:51:38

标签: android android-fragments google-maps-android-api-2

我已在ViewPager中整合了Google地图,之后,当我按下后退按钮关闭我的应用时,我收到“遗憾的应用已关闭”消息。如果有人经历过问题并得到解决,请告诉我如何解决。

代码

public class NearestBathroomMapView extends Fragment {

//Declare all necessary objects
public static View view;
public static GoogleMap gMap;
public static Double latitude,longitude;

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

    if(container==null){
        return null;
    }

    //Inflate the view  
    view= inflater.inflate(R.layout.bathroomsmapview,
            container, false);

    latitude=27.706750;//declare latitude and logitude
    longitude=85.314513;

    setUpIfMapNeeded();

    //call this function to obtain SupportMapFragment

    return view;
}

//Get the map from SupportMapFragment
public void setUpIfMapNeeded(){
    // Try to obtain the SupportMapFragment if map does not equal null. 
    if (gMap == null) {
        gMap = ((SupportMapFragment) BathroomInformation.fragmentManager
                .findFragmentById(R.id.location_map)).getMap();

        //call this function to display map  
        if (gMap != null){  
            setUpMap();
        }
    }
}

private void setUpMap() {
    gMap.setMyLocationEnabled(true);

    //add marker in map   
    gMap.addMarker(new MarkerOptions().position(new LatLng(latitude, longitude)).title("My Home").snippet("Home Address"))  

    //setup map position and animate it
    gMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(latitude,
            longitude), 12.0f));
}

public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

    //view created to display map
    if (gMap != null)
        setUpMap();

    //get SupportMapFragment if map is equal to null
    if (gMap == null) {
    gMap = ((SupportMapFragment) BathroomInformation.fragmentManager
            .findFragmentById(R.id.location_map)).getMap();

    //set up map if map is not null    
    if (gMap != null)
        setUpMap();
    }
}

//should I need to call this below function onBackPressed method ??
public void onDestroyView() {
    super.onDestroyView();
    //map fragment get remove from FragmentManager          
    if (gMap != null) {
        BathroomInformation.fragmentManager.beginTransaction()
                .remove(BathroomInformation.fragmentManager
                        .findFragmentById(R.id.location_map)).commit();
        gMap = null;
    }
}

}

1 个答案:

答案 0 :(得分:1)

我认为你应该消除你的

public void onDestroyView()

功能。我没有看到正确删除被破坏的碎片。

希望它有所帮助。