我创建了一个应用程序,其中包含一个导航抽屉。其中有一个与谷歌地图片段相关联的片段,但如果我通过停留在同一片段上两次点击它而崩溃我使用此方法:
@Override
public void onDestroyView() {
// TODO Auto-generated method stub
Fragment f=(Fragment)getFragmentManager().findFragmentById(R.id.maps);
if(f!=null)
{
getFragmentManager().beginTransaction().remove(f).commit();
}
super.onDestroyView();
}
上面的方法有助于让应用程序在切换到其他片段时不会崩溃,但如果我点击谷歌地图片段,留在谷歌地图的相同片段(谷歌地图片段)整个应用程序崩溃。 /强>
这是我的全部代码:
public class MainActivityUseful extends Fragment{
GoogleMap gmap;
MapView mView;
Bundle mBundle;
//Marker userMarker;
static View rootView;
private static final LatLng venue=new LatLng(26.785634800000000000, 80.922847499999990000);
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
rootView=inflater.inflate(R.layout.activity_main_activity_useful, container,false);
try{
if(gmap==null){
gmap=((MapFragment)getFragmentManager().findFragmentById(R.id.maps)).getMap();
}
gmap.addMarker(new MarkerOptions().position(venue).title("Lucknow Mahotsav Venue").snippet("Venue of Lucknow Mahotsav 2014"));
CameraPosition cmps=new CameraPosition.Builder().target(venue).zoom(15).bearing(70).tilt(25).build();
gmap.animateCamera(CameraUpdateFactory.newCameraPosition(cmps));
}
catch(Exception e)
{
e.printStackTrace();
}
return rootView;
}
@Override
public void onDestroyView() {
// TODO Auto-generated method stub
Fragment f=(Fragment)getFragmentManager().findFragmentById(R.id.maps);
if(f!=null)
{
getFragmentManager().beginTransaction().remove(f).commit();
}
super.onDestroyView();
}
@Override
public void onPause() {
// TODO Auto-generated method stub
super.onPause();
if(gmap!=null)
{
gmap=null;
}
}
@Override
public void onResume() {
// TODO Auto-generated method stub
super.onResume();
if(gmap!=null)
{
gmap=null;
}
}
@Override
public void onStart() {
// TODO Auto-generated method stub
super.onStart();
if(gmap!=null)
{
gmap=null;
}
}
@Override
public void onDetach() {
// TODO Auto-generated method stub
super.onDetach();
if(gmap!=null)
{
gmap=null;
}
}
}
XML如下:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="think.computers.lucknowmahotsav.MainActivityUseful" >
<fragment
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/maps"
android:name="com.google.android.gms.maps.MapFragment"
/>
错误是它说“11-25 12:19:01.628:E / AndroidRuntime(9031):android.view.InflateException:二进制XML文件行#11:错误膨胀类片段”