分からないこと
ViewPagerを使用して、そのうちの一つにGoogleMapを表示したいと考えています。
そこでPagerViewAdapterに入れるFragment(MapTestFragment.java)を作成したのですが、
その中でSupportMapFragmentのインスタンスが作成されずnullが検出されます。(下記コード)
どうすればインスタンスを取得できるのでしょうか?
もし、実装方法が違うのであればそれも一緒に教えてください。
※対象はAndroid3.0以上を考えています。
ソースコード
MapTestFragment.java (Fragment)
java
1import android.net.Uri; 2import android.os.Bundle; 3import android.support.v4.app.Fragment; 4import android.view.LayoutInflater; 5import android.view.View; 6import android.view.ViewGroup; 7import com.google.android.gms.maps.GoogleMap; 8import com.google.android.gms.maps.OnMapReadyCallback; 9import com.google.android.gms.maps.SupportMapFragment; 10import com.google.android.gms.maps.model.LatLng; 11 12public class MapTestFragment extends Fragment implements OnMapReadyCallback { 13 14---略--- 15 16 @Override 17 public View onCreateView(LayoutInflater inflater, ViewGroup container, 18 Bundle savedInstanceState) { 19 View view = inflater.inflate(R.layout.fragment_map_test, container, false); 20 21 SupportMapFragment mapFragment = (SupportMapFragment) getActivity().getSupportFragmentManager() 22 .findFragmentById(R.id.map); 23 mapFragment.getMapAsync(this); // ここでNullPointerExceptionが発生します 24 25 return view; 26 } 27 28---略--- 29 30}
追記
fragment_map_test.xml (layoutファイル)
xml
1<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 tools:context=".MapTestFragment"> 6 7---略--- 8 9 <fragment 10 xmlns:android="http://schemas.android.com/apk/res/android" 11 android:id="@+id/map" 12 android:layout_width="match_parent" 13 android:layout_height="match_parent" 14 android:name="com.google.android.gms.maps.SupportMapFragment"/> 15 16</FrameLayout>
回答1件
あなたの回答
tips
プレビュー