谷歌地图V2在片段崩溃

时间:2014-09-15 12:24:58

标签: android google-maps android-fragments fragment

我正在使用导航抽屉,所以我的整个应用程序都是碎片。第一次,如果我打开窗户 - 一切正常。但是,如果我选择导航去设置然后回来 - 一切都崩溃或我得到空窗口。日志显示 - android.view.InflateException:二进制XML文件行#7:错误膨胀类片段。我试图将android:name改为class,但没有任何改变。试图将MapFragment更改为SupportFragment。一样。由于抽屉,我无法将 extends Fragment 更改为扩展FragmentActivity

这是我的代码

public class EmployeeMyMove extends Fragment {
      View view; 
      private GoogleMap googleMap;
      MarkerOptions marker, emmarker;
      Location location;

   public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

        if (view != null) {
            ViewGroup parent = (ViewGroup) view.getParent();
            if (parent != null)
                parent.removeView(view);
        }
        try {
            view = inflater.inflate(R.layout.employee_my_move, container, false);
            Log.d("Log", "Tryed");
        } catch (InflateException e) {              
            Log.d("Log", "crashed "+e);
        }
        initilizeMap();


        return view;
   }

   private void initilizeMap() {
    //if (googleMap == null) {
        /*latitudeMine = glob.getLat();
        longitudeMine = glob.getLng();*/

        googleMap = ((MapFragment) getFragmentManager().findFragmentById(
                R.id.MyMoveMap)).getMap();

        marker = new MarkerOptions().position(new LatLng(0, 0)).title("You are here");
        googleMap.addMarker(marker);

        CameraPosition cameraPosition = new CameraPosition.Builder().target(
                new LatLng(latitudeMine, longitudeMine)).zoom(15).build();

        googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
    //}
}

}

并查看:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

    <fragment
        android:id="@+id/MyMoveMap"
        android:name="com.google.android.gms.maps.MapFragment"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1" />


</LinearLayout>

有关修复的任何建议吗?

2 个答案:

答案 0 :(得分:1)

You should override 
public void onDestroy() {
    parentView.removeView(this.mMapView);
    super.onDestroy();
    }
Take a look at here

Android mapview with fragments can't be added twice?

答案 1 :(得分:0)

这是完全修复(对所有需要帮助的人)

<强> 1。对于创建抽屉的主要活动:

  1. 确保创建抽屉的主要活动延伸 FrameActivity
  2. 创建变量 - 公共静态 android.support.v4.app.FragmentManager fragmentManager;
  3. 分配 - fragmentManager = getSupportFragmentManager();
  4. <强> 2。你的片段:

    1. 指定googleMap,例如 - googleMap =((SupportMapFragment) YourMainActivity.fragmentManager                     .findFragmentById(R.id.location_map))的GetMap();
    2. 最重要 - 覆盖:
    3. @覆盖

      public void onDestroyView() {
          super.onDestroyView();
          if (googleMap != null) {
              YourMainActivity.fragmentManager.beginTransaction()
                  .remove(YourMainActivity.fragmentManager.findFragmentById(R.id.location_map)).commit();
              googleMap = null;
          }
      }
      

      感谢Gaurav的想法。帮助找到答案。谢谢!