2014-10-24 3 views
0

У меня есть Google Maps в ViewPager, а потом, когда я закрываю приложение, нажимая кнопку «Назад», я получаю сообщение «К сожалению приложение закрыто». Если кто-то прошел проблему и решится, пожалуйста, дайте мне знать, как ее решить.Приложение падает, когда я закрываю приложение

Код

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; 
    } 
} 

} 
+0

Что скажете? – 323go

ответ

1

Я думаю, вы должны устранить вашу функцию

public void onDestroyView() 

. Я не вижу правильного удаления фрагмента, который был уничтожен.

Надеюсь, это поможет.

Смежные вопросы