【问题标题】:Google map V2 in fragment crash谷歌地图 V2 在片段崩溃
【发布时间】:2014-09-15 12:24:57
【问题描述】:

我正在使用导航抽屉,所以我的整个应用程序都在片段中。如果我第一次打开窗户 - 一切正常。但是,如果我从导航中选择进入设置然后返回 - 一切都会崩溃或我得到空窗口。日志显示 - android.view.InflateException: Binary XML file line #7: Error inflating class fragment。我试图将 android:name 更改为 class,但没有任何变化。试图将 MapFragment 更改为 SupportFragment。一样。由于抽屉,我无法将 extends Fragment 更改为 extends 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>

对修复有什么建议吗?

【问题讨论】:

  • 09-15 15:49:03.421: D/Log(21533): crashed android.view.InflateException: Binary XML file line #7: Error inflating class fragment
  • 是的。瞬间崩溃。即使是第一次崩溃。

标签: android google-maps android-fragments fragment


【解决方案1】:
你应该重写
public void onDestroy() {
    parentView.removeView(this.mMapView);
    super.onDestroy();
    }
看这里

Android mapview with fragments can't be added twice?

【讨论】:

  • 可悲的是——同样的结局。空布局
  • 今天我发现wrole“崩溃”问题是整个地图碎片。我将其销毁,但无法再次创建。将尝试以编程方式创建。谢谢。
【解决方案2】:

这里是完整的修复(所有需要帮助的人)

1.到创建抽屉的主要活动:

  1. 确保创建抽屉的主要活动扩展 FrameActivity
  2. 创建变量 - 公共静态 android.support.v4.app.FragmentManager fragmentManager;
  3. 分配它 - fragmentManager = getSupportFragmentManager();

2。您的片段:

  1. 分配 googleMap like - googleMap = ((SupportMapFragment) YourMainActivity.fragmentManager .findFragmentById(R.id.location_map)).getMap();
  2. 最重要的 - 覆盖:

@Override

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

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

【讨论】:

    猜你喜欢
    • 2015-06-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多