我在我的应用程序中使用 Fragment(with TabHost)。第二次打开我的 SupportMapFragment (Android maps v2) 时,我收到以下错误:
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)
我的主要片段类:
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 文件:
<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" />
片段1类:
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();
}
}