10

SupportMapFragment動的に作成してFrameLayoutコンテナに入れようとしています。

私の問題は、それmMapFragment.getMap()が返されることnullです...

誰でも助けることができますか?

CenterMapFragment.java

public class CenterMapFragment extends Fragment {

    private SupportMapFragment mMapFragment;

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

        return inflater.inflate(R.layout.center_map_fragment, container, false);
    }

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

        if (GooglePlayServicesUtil.isGooglePlayServicesAvailable(getActivity()) == ConnectionResult.SUCCESS) {
            setUpMapIfNeeded();
        }
    }


    private void setUpMapIfNeeded() {

        if (mMapFragment == null) {

            mMapFragment = SupportMapFragment.newInstance();
            FragmentTransaction fragmentTransaction =
                            getChildFragmentManager().beginTransaction();
            fragmentTransaction.replace(R.id.map, mMapFragment);
            fragmentTransaction.commit(); 

            setUpMap();
        }
    }

    private void setUpMap() {       

        GoogleMap map = mMapFragment.getMap();  

        // map is null!

    }

    @Override
    public void onResume()
    {
        super.onResume();
        setUpMapIfNeeded();     
    }
}

center_map_fragment.xml

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

    <FrameLayout
        android:id="@+id/map"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    </FrameLayout>

    <Button
        android:id="@+id/btn_loc"
        android:layout_width="60dp"
        android:layout_height="50dp"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:background="@drawable/locbtn" />

</RelativeLayout>
4

3 に答える 3

8

commit()FragmentTransactionその操作をすぐに実行しません。を呼び出すまでにsetUpMap()onCreateView()は で呼び出されてSupportMapFragmentいないため、マップはまだありません。

1 つのアプローチは、ネストされたフラグメントを使用せず、代わりにCenterMapFragmentextendsSupportMapFragmentを選択するgetMap()ことonCreateView()ですonActivityCreated()

于 2013-05-01T16:24:21.540 に答える
2

次のリンクでは、CommonsWare がコメントの最後の部分で示唆しているように、MapView が使用されています。

http://ucla.jamesyxu.com/?p=287

public class MapFragment extends Fragment {

    MapView m;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
            Bundle savedInstanceState) {
        // inflat and return the layout
        View v = inflater.inflate(R.layout.map_fragment, container, false);
        m = (MapView) v.findViewById(R.id.mapView);
        m.onCreate(savedInstanceState);

        return v;
    }

    @Override
    public void onResume() {
        super.onResume();
        m.onResume();
    }

    @Override
    public void onPause() {
        super.onPause();
        m.onPause();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        m.onDestroy();
    }

    @Override
    public void onLowMemory() {
        super.onLowMemory();
        m.onLowMemory();
    }
}

これらがお役に立てば幸いです。

于 2013-09-29T15:50:07.540 に答える
1

通話中

fragmentTransaction.commit();

非同期であるため、マップから戻ったとき、マップはまだ準備ができていません。電話するだけ

getFragmentManager().executePendingTransactions();

次の行として、これにより同期が行われ、次の命令で実行されて、完成したマップが正常に取得されます。かかる時間が心配な場合は、初期化全体を に入れ、AsyncTaskマップの初期化中に進行状況インジケーターをユーザーに表示します。

于 2014-12-18T10:37:05.053 に答える