SupportMapFragment无法添加到FrameLayout

时间:2017-02-01 12:54:04

标签: android google-maps android-fragments

  

我已将mapFragment添加到活动的onCreate中的FrameLayout,如

getSupportFragmentManager().beginTransaction()
                .replace(R.id.frame_container,//where frame_container is a FrameLayout
                 new MapyFragment(),
                 MapyFragment.class.getSimpleName()).commit();

和frame_container是FrameLayout

MapyFragment.class

public class MapyFragment extends Fragment {

    private SupportMapFragment supportMapFragment;
    private GoogleMap map;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        setRetainInstance(true);
        setHasOptionsMenu(true);

        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_mapy, container, false);
    }

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

        FragmentManager fm = getChildFragmentManager();/// getActivity().getSupportFragmentManager();/// getChildFragmentManager();
        supportMapFragment = (SupportMapFragment) fm.findFragmentById(R.id.map_container);
        if (supportMapFragment == null) {
            supportMapFragment = SupportMapFragment.newInstance();
            fm.beginTransaction().replace(R.id.map_container, supportMapFragment).commit();
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        if (supportMapFragment != null) {
            supportMapFragment.getMapAsync(new OnMapReadyCallback() {

                @Override
                public void onMapReady(GoogleMap googleMap) {
                    map = googleMap;
                    map.animateCamera(CameraUpdateFactory.zoomTo(15));
                }
            });
        }
    }
}

我尝试使用getChildFragmentManager和getActivity.getSupportFragmentManager()进行更改。

谷歌地图没有显示。没有崩溃,我做错了什么。请澄清并

为什么我们必须使用fragmentManger来获取map_container。参见

fm.findFragmentById(R.id.map_container);

fragment_mapy.xml

<fragment
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:map="http://schemas.android.com/apk/res-auto"
        android:id="@+id/map_container"
        class="com.google.android.gms.maps.SupportMapFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginBottom="40dp"
        map:uiZoomControls="true" />

2 个答案:

答案 0 :(得分:0)

试试这个:

#include <iostream> #include <fstream> #include <string> using namespace std; int main() { int count = 0; double amount, mileage; string date, brand; char ans; cout << "****************************" << endl; cout << "CAR FUEL CONSUMPTION HISTORY" << endl; cout << "****************************" << endl; for (;;) { cout << "\nDate (dd/mm/yy): "; cin >> date; cout << "Amount Payable: $"; cin >> amount; cout << "Brand: "; cin >> brand; cout << "Mileage (km): "; cin >> mileage; cout << "\nContinue? (Y/N): "; cin >> ans; if (ans == 'N' || ans == 'n') { break; } else { count++; } } ofstream openFile; openFile.open("conHist.txt"); // NOT WORKING!!!! >:( while(openFile.good()) { openFile << "Entry " << count << endl;; openFile << "Date (dd/mm/yy): " << date << endl; openFile << "Amount Payable: $" << amount << endl; openFile << "Brand: " << brand << endl; openFile << "Mileage: " << mileage << "km" << endl; openFile.close(); } return 0; } 中初始化您的supportMapFragment

onCreateview
布局xml中的

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

    super.onCreateView(inflater, container, savedInstanceState);
    View rootView = inflater.inflate(R.layout.activity_map, container, false);
    smp = (SupportMapFragment) this.getChildFragmentManager().findFragmentById(R.id.map);
    smp.getMapAsync(this);
    retrun rootView;
}

@Override
public void onMapReady(GoogleMap map) {
    mMap = map;
}

另请阅读SupportMapFragment此处

答案 1 :(得分:0)

解决我自己的问题

首先,没有区别 b / w android:name&amp; android:class两者都运行正常。 See this

getChildFragmentManager()是罪魁祸首。

  

最新工人阶级

public class MapyFragment extends Fragment implements OnMapReadyCallback {

    private SupportMapFragment supportMapFragment;
    private GoogleMap map;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        setRetainInstance(true);
        setHasOptionsMenu(true);
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_mapy,container,false);
    }

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

        FragmentManager fm = getActivity().getSupportFragmentManager();/// getChildFragmentManager();
        supportMapFragment = (SupportMapFragment) fm.findFragmentById(R.id.map_container);
        if (supportMapFragment == null) {
            supportMapFragment = SupportMapFragment.newInstance();
            fm.beginTransaction().replace(R.id.map_container, supportMapFragment).commit();
        }
    }

    @Override
    public void onMapReady(GoogleMap googleMap) {
        map = googleMap;
        map.animateCamera(CameraUpdateFactory.zoomTo(15));
    }
}