使用后退按钮进入时,地图片段崩溃

时间:2016-07-12 10:55:41

标签: android google-maps-android-api-2 inflate-exception

我有一个基于NavigationDrawer的应用程序。我加载的其中一个片段包含一个GoogleMap片段。

这是加载片段

的用途
getSupportFragmentManager().beginTransaction().replace(R.id.container, 
   new MyMapFragment()).addToBackStack("mytag1").commit();

页面加载没有任何问题并显示地图。但是当我使用菜单切换到另一个片段并按下后退按钮时应用程序崩溃。该片段仅包含加载地图的最少代码。

片段XML:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/ll_report_in_out"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/white"
    android:orientation="vertical"
    tools:context="my.package.name" >

        <LinearLayout
            android:id="@+id/llv_layer2"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:orientation="vertical" 
        >
            <!-- some content -->
        </LinearLayout>

        <fragment
            android:id="@+id/map"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            class="com.google.android.gms.maps.SupportMapFragment"
            android:name="my.package.name.MyMapFragment"
        />
</LinearLayout>

活动:

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.fragment_report_in_out, container, false);

        try {
            fm = ((SupportMapFragment) getChildFragmentManager()
                    .findFragmentById(R.id.map));

            if (fm != null)
                map = fm.getMap();
            else
                map = null;
        }
        catch(Exception e)
        {   Log.i("dbg",e.toString()); }                         
}

上面的Catch不会发射。 Logcat在第125行说明了膨胀异常,这是放置地图片段的地方。

我真的很感激任何帮助...

我正在添加我当前的测试条件和工作环境以防万一

AndroidStudio 2.1 Min SDK Ver 10 目标SDK版本24 编译SDK Ver 24

在运行Android 6.0.1的Nexus 6上进行测试

2 个答案:

答案 0 :(得分:0)

直接使用MapView而不是xml文件中的Fragment

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:scrolling_image_view="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".screens.DashBoardFragment">
    <!-- Component 1 to cover half screen height -->
    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="0.50"
        android:layout_gravity="top"
        >

        <EditText android:id="@+id/url_live_target"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textSize="@dimen/textSizeNormal"
                android:inputType="textUri"
                android:singleLine="true"
                android:hint="Receive Live Update URL" />
    </FrameLayout>

    <!-- Component 2 to cover half screen height -->
    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="0.50"
        >

        <com.google.android.gms.maps.MapView
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/map_dashBoard"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="bottom"
            />
            </FrameLayout>
</LinearLayout>

现在在你的片段中

    private MapView mMapView;
    private GoogleMap mGoogleMap;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view= inflater.inflate(R.layout.fragment_dash_board, container, false);

        mMapView = (MapView) view.findViewById(R.id.map_dashBoard);
        mMapView.onResume();

        try {
            MapsInitializer.initialize(getActivity().getApplicationContext());
        } catch (Exception e) {
            e.printStackTrace();
        }
        mGoogleMap = mMapView.getMap();

    }

您可能需要根据您的具体要求编辑逻辑。 [片段添加/替换等]

答案 1 :(得分:0)

试试这段代码。这解决了我的问题

View view = null;

在onCreateView()中检查此条件

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
 if(view != null)
 {
   ViewGroup parent = (ViewGroup) container;
   if(parent !=null)
   {
     parent.removeView(view);
   }
 }
 try
 {
  view = inflater.inflate(R.layout.fragment_report_in_out, container, false);
 }
 catch(Exception e){}
 return view;
}

希望这有帮助!