2013-05-01 16 views
10

SupportMapFragmentを動的に作成し、FrameLayoutコンテナに挿入しようとしています。SupportMapFragmentのgetMap()がnullを返します

私の問題はmMapFragment.getMap()戻っnullが...

誰を助けることができるということですか? FragmentTransaction

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> 

答えて

8

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

一つのアプローチはCenterMapFragmentgetMap()onCreateView()(例えば、onActivityCreated())後の任意の時間に動作する必要があり、その場合SupportMapFragmentを拡張有する代わりに選出、ネストされた断片を使用しないことです。 CommonsWareが彼のコメントの最後の部分に示唆するように、以下のリンクのMapViewで

+0

'CenterMapFragment'が' SupportMapFragment'を拡張する場合、どのようにマップを 'FrameLayout'に設定できますか?私はレイアウトを変更する必要がありますか? – jul

+1

@jul:1つのアプローチでは、 'CenterMapFragment'が、' SupportMapFragment'が作成したものを得るために 'super.onCreateView()'を最初に呼び出した 'onCreateView()'を実装しています。次に、 'FrameLayout'を作成し、' Super'で作成した 'View'を' FrameLayout'の子として追加します。そして 'onCreateView()'から 'FrameLayout'を返します。 'SupportMapFragment'の内容は、' CenterMapFragment'が望むものであれば、それをラップします。あるいは、 'CenterMapFragment'を通常の' Fragment'だけにし、 'SupportMapFragment'の代わりに' MapView'を使用してください。あるいは、あなたの現在のコードで 'setUpMap()'を呼び出すより良い時期を見つけてください。 – CommonsWare

+0

私は 'MapView'を使用します。 – jul

2

が使用されます。

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(); 
    } 
} 

私はこれらが役に立つことを願っています。

+0

このリンクは質問に答えるかもしれませんが、答えの本質的な部分をここに含めて参考にしてください。リンクされたページが変更された場合、リンクのみの回答は無効になります。 – skolima

+0

あなたは正しいです!ありがとう – UserK

1

呼び出す

fragmentTransaction.commit(); 

は、あなたがそれから戻ったときのマップは、まだ準備ができていないだろう、非同期です。単にそれは同期になりますし、すべての権利、完成マップを拾うために次の命令のためにそれを実行します、次の行、など

getFragmentManager().executePendingTransactions(); 

を呼び出します。それにかかる時間が心配な場合は、初期化全体をAsyncTaskに入れ、マップの初期化中に進捗インジケーターをユーザーに表示してください。

関連する問題