应用程序崩溃“返回”映射片段

时间:2016-04-01 13:10:39

标签: android google-maps android-fragments fragment-backstack

我正在处理我的第一个应用程序并遇到了“后退”功能的问题。

当我打开应用程序时,单击菜单中的另一个片段(2,3或4),然后按下后退按钮,应用程序崩溃。当我启动应用程序并单击菜单中的3 > 2 > crash(按此顺序)并按后退按钮时,我将返回fragmentTransaction.replace(R.id.fr_content_container, new NewsFragment()).addToBackStack(null).commit(); 。当我回到包含谷歌地图的mainFragment时,应用程序崩溃了。

我认为我的问题与此片段中的googlemap(要导航回去的片段)有关,因为当我设置2在该视图的xml中的错误行之前输入并再次运行时,错误行号是+2。当我注释掉这个片段时,后退按钮工作正常。

通过导航到我打电话的新片段:

04-01 14:26:39.679 19054-19054/aaeu.app E/AndroidRuntime: FATAL EXCEPTION: main
                                                          Process: aaeu.app, PID: 19054
                                                          android.view.InflateException: Binary XML file line #7: Binary XML file line #7: Error inflating class fragment
                                                              at android.view.LayoutInflater.inflate(LayoutInflater.java:539)
                                                              at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
                                                              at aaeu.app.presentationlayer.tab_AlertMapsOverview.onCreateView(mapsFragment.java:52)
                                                          .....................

我得到的错误是

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

        <fragment
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="2"
            android:name="com.google.android.gms.maps.SupportMapFragment"
            android:id="@+id/mapwhere" />
    </RelativeLayout>

mapsFragment的XML是:

    private SupportMapFragment mMapFragment;
    private AlertManager mAlertManager;
    private GoogleMap mGoogleMap;
    HashMap<Marker, Alert> mMarkerMap;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mAlertManager = new AlertManager(getContext());
    }

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

        loadMap();

        return view;
    }

    private void loadMap() {
        mMapFragment = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.mapwhere);
        if (mMapFragment == null) {
            FragmentManager fragmentManager = getFragmentManager();
            FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
            mMapFragment = SupportMapFragment.newInstance();
            fragmentTransaction.replace(R.id.mapwhere, mMapFragment).commit();
        }

        if (mMapFragment != null) {
            mMapFragment.getMapAsync(new OnMapReadyCallback() {
                @Override
                public void onMapReady(GoogleMap googleMap) {
                    if (googleMap != null) {
                        googleMap.getUiSettings().setAllGesturesEnabled(true);

                        LatLng marker_latlng = new LatLng(55.042684, 14.125549);

                        CameraPosition cameraPosition = new CameraPosition.Builder().target(marker_latlng).zoom(3.0f).build();
                        CameraUpdate cameraUpdate = CameraUpdateFactory.newCameraPosition(cameraPosition);
                        googleMap.moveCamera(cameraUpdate);

                        setGoogleMap(googleMap);
                    }
                }
            });
        }
    }

    private void setGoogleMap(GoogleMap googleMap) {
        this.mGoogleMap = googleMap;
    }


    // This method is called by the asynctask who downloads the alerts
    @Override
    public void UpdateAlerts(List<Alert> alerts) {

        mMarkerMap = new HashMap<Marker, Alert>();
        try {
        for (Alert alert : alerts) {
            MarkerOptions marker = new MarkerOptions();
            marker.position(alert.Location.get(0));
            marker.title(alert.GetFullName());

            Marker m = mGoogleMap.addMarker(marker);
            mMarkerMap.put(m, alert);
        }


            mGoogleMap.setOnMarkerClickListener(
                    new GoogleMap.OnMarkerClickListener() {
                        @Override
                        public boolean onMarkerClick(Marker marker) {
                            startDetailView(marker);
                            return false;
                        }
                    }
            );
        }
        catch (NullPointerException e)
        {
            Log.i("tab_AlertMapsOverview","No missings to create markers for or set clicklisteners on. Are there missings in the configured Areas?");
        }
    }

地图片段的代码是:

import createOneShotMiddleware from 'redux-middleware-oneshot';
import { NetInfo } from 'react-native';
import { checkConnection } from '../actions/networkActions';

export const middleware = createOneShotMiddleware((dispatch) => {
    const handle = (isConnected) => dispatch(checkConnection(isConnected));
    NetInfo.isConnected.fetch().done(handle);
    NetInfo.isConnected.addEventListener('change', handle);
});

在这个问题附近我也有一点问题。当我有3个片段时,1,2和3.当我(例如)从活动1开始并导航到2&gt; 3> 2&gt; 3我需要再推4次才能回来1.使用一种“树”系统进行后退导航是否可行。所以我总是上升1级?也许这清除了我的意思。

this 我认为目前的情况远非最佳,因为片段仍然在RAM中活动?或者我错了?

2 个答案:

答案 0 :(得分:2)

我也有同样的问题。在浪费了大量时间寻找解决方案之后,我最终做了什么,我从xml中删除了碎片标签。把FrameLayout放在那里,每当我想添加那个片段时,我会用片段事务添加它。这对我有用。

问题的原因是,每当调用该片段的onCreateView()时,xml文件将被膨胀,这将使片段膨胀。这将导致具有相同id的多个片段导致此异常。

答案 1 :(得分:0)

用这个替换你的 onBackPressed...

 @Override
    public void onBackPressed() {
        // if there is a fragment and the back stack of this fragment is not empty,
        // then emulate 'onBackPressed' behaviour, because in default, it is not working
        FragmentManager fm = getSupportFragmentManager();
        for (Fragment frag : fm.getFragments()) {
            if (frag.isVisible()) {
                FragmentManager childFm = frag.getChildFragmentManager();
                if (childFm.getBackStackEntryCount() > 0) {
                    childFm.popBackStack();
                    return;
                }
            }
        }
        super.onBackPressed();
    }