hi i have to implement Google map in my application. In my application i have 3 tabs in which one of the tab display this Map. but my problem is when i first time go to Map fragment it's working fine but when suddenly go to another tab and then return back to Map Fragment my app got crash? with below logcat.

Layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="55dp"
android:background="@drawable/button_bar_gradient"
android:orientation="vertical" >

<fragment
    android:id="@+id/map1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    class="com.google.android.gms.maps.SupportMapFragment" />

</LinearLayout>

Logcat

enter image description here

Pleas some one help me on this.

有帮助吗?

解决方案 2

Try to remove Fragment in onDestroyView(....) event of your Fragment like

 @Override
    public void onDestroyView ()
    {
        try{
          SupportMapFragment fragment = ((SupportMapFragment) getFragmentManager().findFragmentById(R.id.map1));
          FragmentTransaction ft = getActivity().getSupportFragmentManager().beginTransaction();
          ft.remove(fragment);
          ft.commit();
        }catch(Exception e){
        }
      super.onDestroyView();  
  }

And give me feedback on this.

其他提示

As Simple Plan told, you need to remove the map in your onDestroyView. You can do this like this:

@Override
public void onDestroyView() {
        SupportMapFragment mapFragment = ((SupportMapFragment) getActivity()
                .getSupportFragmentManager().findFragmentById(R.id.google_map));

        if(mapFragment != null) {
            FragmentManager fM = getFragmentManager();
            fM.beginTransaction().remove(mapFragment).commit();
        }
      super.onDestroyView(); 
    }

If this doesn't work try this post: Error opening SupportMapFragment for second time

Instead of declaring de SupportMapFragment in layout, do it programatically and be sure you use getChildFragmentMananger instead of the classic getFragmentManager() to create the fragment.

mMapFragment = SupportMapFragment.newInstance();
    FragmentTransaction fragmentTransaction =
             getChildFragmentManager().beginTransaction();
     fragmentTransaction.add(R.id.map_root, mMapFragment);
     fragmentTransaction.commit(); 

Keep this SupportMapFragment mMapFragment as you will need it to retrieve the GoogleMap object:

 GoogleMap map = mMapFragment.getMap();
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top