【问题标题】:Map Fragment crashes when entering with back button使用后退按钮进入时地图片段崩溃
【发布时间】:2016-07-12 10:55:40
【问题描述】:

我有一个基于 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 行说 inflate 异常,这是放置地图片段的位置。

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

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

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

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

【问题讨论】:

  • onCreateView 方法在返回 Fragment 时是否被调用?
  • @ρяσѕρєя K 是的,当我按下返回按钮时,会再次调用 onCreateView。

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


【解决方案1】:

尝试直接使用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();

    }

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

【讨论】:

  • 我无法做到这一点,你可以从布局中看出,我需要在地图上方显示一些内容。
  • 即使我加载包含地图视图的片段,您的代码段也会崩溃,并出现以下错误 Attempt to invoke interface method 'void maps.af.yo()' on an null object reference。跨度>
  • @Deepak:我能详细说明一下吗? “空地图.af.y.o()”
  • 抱歉,这正是 LogCat 中显示的错误消息,就在“FatalException”之后。我什至尝试将您建议的代码段放在 onResume 和 onAttach 方法中。我仍然收到相同的错误消息。
【解决方案2】:

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

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;
}

希望这会有所帮助!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-13
    • 1970-01-01
    相关资源
    最近更新 更多