当父项活动开启时,Mapment内部的MapFragmentPause MapFragment变为null

时间:2015-02-26 12:51:07

标签: android

我有一项活动ACTIVITY => Fragment => MapFragment

on onPause of ACTIVITY MapFragment变为null。

以下是我正在使用的方法:

 private MapFragment getMapFragment() {
        FragmentManager fm = null;

        Log.d("", "sdk: " + Build.VERSION.SDK_INT);
        Log.d("", "release: " + Build.VERSION.RELEASE);

        if (Build.VERSION.SDK_INT < 21) {
            Log.d("framentmgr", "using getFragmentManager");
            fm = getFragmentManager();
        } else {
            Log.d("framentmgr", "using getChildFragmentManager");
            fm = getChildFragmentManager();
        }
//        fm = getChildFragmentManager();
        fm.beginTransaction();

        MapFragment mapFragment = (MapFragment) fm.findFragmentById(R.id.fragment_map);


        return mapFragment;
    }

出现这种情况的任何解决方案或原因?

1 个答案:

答案 0 :(得分:-1)

谢谢,但我得到同样的错误。所以我现在通过将MapFragment作为另一个类中的公共静态来解决它。

public static MapFragment mapFragment;

并在Fragment类中

private MapFragment getMapFragment() {
        FragmentManager fm = null;

        Log.d("", "sdk: " + Build.VERSION.SDK_INT);
        Log.d("", "release: " + Build.VERSION.RELEASE);

        if (Build.VERSION.SDK_INT < 21) {
            Log.d("framentmgr", "using getFragmentManager");
            fm = getFragmentManager();
        } else {
            Log.d("framentmgr", "using getChildFragmentManager");
            fm = getChildFragmentManager();
        }
//        fm = getChildFragmentManager();
        fm.beginTransaction();

        MapFragment mapFragment = null;
        if(MainActivity.mapFragment==null)
            mapFragment = (MapFragment) fm.findFragmentById(R.id.fragment_map);
        else
            return MainActivity.mapFragment;


        return mapFragment;
    }