Android - SupportMapFragment error when opening for second time

半世苍凉 提交于 2019-12-11 02:14:36

问题


I'm using Fragment(with TabHost) in my application. When opening my SupportMapFragment (Android maps v2) for a second time, I get the following error:

FATAL EXCEPTION: main
android.view.InflateException: Binary XML file line #48: Error inflating class fragment
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
at android.view.LayoutInflater.inflate(LayoutInflater.java:396)

My main Fragment Class:

FragmentManager fm = getFragmentManager();
    if (tabId.equals("1"))
         fm.beginTransaction().replace(R.id.fragment1, new Fragment1(), tabId).commit();

    if (tabId.equals("2"))
            fm.beginTransaction().replace(R.id.fragment2, new Fragment2(), tabId).commit();

XML File:

<fragment
    android:id="@+id/mapview"
    android:layout_width="match_parent"
    android:layout_height="342dp"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    class="com.google.android.gms.maps.SupportMapFragment" />

Fragment1 class:

public class Fragment1 extends SupportMapFragment {

public void onCreate(Bundle arg0) {
    super.onCreate(arg0);
}

public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View view = super.onCreateView(inflater, container, savedInstanceState);  

    view = inflater.inflate(R.layout.activity_map_event, container, false); // Error occurs in this line when I called second time.
    view.setId(getId());
    SupportMapFragment fm = (SupportMapFragment) getActivity()
            .getSupportFragmentManager().findFragmentById(R.id.mapview);
    mapView = fm.getMap();
    initMap();
    return view;
}
public void onDestroyView() {
    super.onDestroyView();
    SupportMapFragment fragment = (SupportMapFragment) getActivity().getSupportFragmentManager().findFragmentById(R.id.mapview);
    FragmentTransaction ft = getActivity().getSupportFragmentManager().beginTransaction();
    ft.remove(fragment);
    ft.commit();
}
}

来源:https://stackoverflow.com/questions/17722851/android-supportmapfragment-error-when-opening-for-second-time

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!