現在Fragmentの中でGoogleMapを取得しようとしていて
onResumeの中で
lang
1if (mMap == null) { 2 // Try to obtain the map from the SupportMapFragment. 3 SupportMapFragment smp = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.frag_map); 4 mMap = smp.getMap(); 5}
を書いて実行するとSupportMapFragmentがnullになってしまいます。
mMapは上で private GoogleMap mMap;として定義しています。
fragmentのxmlはこのようになっています。
lang
1<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:id="@+id/linear" 4 android:layout_width="match_parent" 5 android:layout_height="match_parent" 6 android:orientation="vertical"> 7 8 <fragment xmlns:android="http://schemas.android.com/apk/res/android" 9 android:id="@+id/frag_map" 10 android:name="com.google.android.gms.maps.MapFragment" 11 android:layout_width="match_parent" 12 android:layout_height="0dp" 13 android:layout_weight="2" 14 tools:context="develop.android.coopi.home.fragment.MapsFragment"/> 15 16 <LinearLayout 17 android:layout_width="match_parent" 18 android:layout_height="0dp" 19 android:layout_weight="1" 20 android:orientation="horizontal"> 21 22 23 <LinearLayout 24 android:layout_width="0dp" 25 android:layout_height="match_parent" 26 android:layout_weight="1" 27 android:gravity="center" 28 android:orientation="horizontal"> 29 30 <TextView 31 android:id="@+id/textview" 32 android:layout_width="wrap_content" 33 android:layout_height="wrap_content" 34 android:text="GPS" 35 android:textSize="20sp" /> 36 37 <ToggleButton 38 android:id="@+id/gps_toggle" 39 android:layout_width="wrap_content" 40 android:layout_height="wrap_content" 41 android:text="New ToggleButton" /> 42 </LinearLayout> 43 44 </LinearLayout> 45 46</LinearLayout> 47
Mapを表示しようとしているFragment名はMapsFragmentでFragmentを配置しようとしているActivity上のソースは下のようになっています。
lang
1FragmentManager fragmentManager = getSupportFragmentManager(); 2FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); 3MapsFragment map_fragment = new MapsFragment(); 4fragmentTransaction.add(R.id.fragment_container, map_fragment); 5fragmentTransaction.commit();
findFragmentByIdでのnullが起こらないようにするにはどうしたらいいでしょうか?
わかりにくい質問になってしまいすみませんが、どうかご回答よろしくお願いいたします。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2015/12/02 08:45
2015/12/02 08:50
2015/12/02 08:59
2015/12/02 09:10
2015/12/02 09:29
2015/12/02 09:36
2015/12/02 09:45
2015/12/02 09:49
2015/12/02 09:57
2015/12/02 10:10
2015/12/02 10:24
2015/12/02 10:37
2015/12/03 03:27
2015/12/03 03:31
2015/12/03 03:47
2015/12/03 06:03
2015/12/03 06:30