返回到上一个地图片段时,膨胀类映射片段错误

时间:2015-02-04 08:51:35

标签: android fragment supportmapfragment

我有一个FragmentActivity,它会替换不同的片段以显示信息。其中一个片段(StepTwoAddressFragment)的布局中包含一个映射。第一次,这个片段正确地绘制了视图,但是当我回到堆栈中的这个片段时,应用程序崩溃了。我需要将此片段添加到片段后台堆栈中。

android.view.InflateException:二进制XML文件行#9:错误膨胀类片段

FragmentActivity

switch (mCurrentSection) {
    case SECTION_STEP_TWO_MAPS_ADDRESS:
        SelectNewPositionFragment fragm = SelectNewPositionFragment.newInstance();
        mCurrentFragment = fragm;
        break;

    case SECTION_STEP_TWO_ADDRESS:
        mCurrentFragment = StepTwoAddressFragment.newInstance();
        break;

    case SECTION_STEP_TWO_WHY_GO:
        mCurrentFragment = StepTwoWhyGoFragment.newInstance();
        break;
}

if (mCurrentFragment != null) {
    FragmentTransaction ft = mFragmentManager.beginTransaction().replace(R.id.main_content_frame, mCurrentFragment);
    if (hasToAddToBackStack()) {
        ft.addToBackStack(null);
    }
    ft.commit();
}

private boolean hasToAddToBackStack() {
    return mCurrentSection != SECTION_STEP_TWO_MAPS_ADDRESS || mCurrentSection != SECTION_STEP_TWO_ADDRESS;
}

fragment_selected_new_position_layout.xml

................

<fragment
    android:id="@+id/map"
    class="com.google.android.gms.maps.SupportMapFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

................

SelectNewPositionFragment

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    super.onCreateView(inflater, container, savedInstanceState);

    v = inflater.inflate(R.layout.fragment_selected_new_position_layout, container, false);

    //mapFragment = ((SupportMapFragment) getActivity().getSupportFragmentManager().findFragmentById(R.id.map));
    mapFragment = ((SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map));
    map = mapFragment.getMap();

}

@Override
public void onDestroyView() {
    super.onDestroyView();
    FragmentTransaction ft = getActivity().getSupportFragmentManager().beginTransaction();
    if (mapFragment.isResumed()) {
        ft.remove(mapFragment);
        ft.commit();
    }
}

我也尝试了这段代码:

@Override
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.map, container, false);
    } catch (InflateException e) {
        /* map is already there, just return view as it is */
    }
    return view;
}

我更改了布局中的片段视图,并使用FrameLayout

进行更改
<FrameLayout
    android:id="@+id/map"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

我创建了一个新类MapBaseFragment,它扩展了SupportMapFragment类并初始化了地图。

public class MapBaseFragment extends SupportMapFragment {

...

@Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        setUpMap();
    }

...

}

和包含地图的片段

mapFragment = new MapBaseFragment();
FragmentTransaction ft = getChildFragmentManager().beginTransaction();
ft.replace(R.id.map, mapFragment);
ft.commit();

最后,删除onDestroyView代码。

它适合我。

1 个答案:

答案 0 :(得分:0)

您始终可以使用FragmentTransaction.add()代替FragmentTransaction.replace()

这将保留包含新片段下方地图的片段。

您可能希望将背景设置为不透明,并使用FragmentTransaction.addToBackStack()

将其添加到后台堆栈