2017-01-05 6 views
2

フラグメント内にGoogleマップを取得しようとしていますが、NullPointerExceptionが発生しています。私は似たような質問のために他の答えを読んだが、彼らは私を助けなかった。フラグメント内のSupportMapFragmentを持つNullPointerException

HomeFragment:

@Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 
     View view = inflater.inflate(R.layout.fragment_home, container, false); 
     SupportMapFragment mapFragment = (SupportMapFragment) getFragmentManager() 
      .findFragmentById(R.id.ftMap); 
     mapFragment.getMapAsync(this); 

     return view; 
    } 

fragment_home.xml:

<fragment 
     android:id="@+id/ftMap" 
     android:name="com.google.android.gms.maps.SupportMapFragment" 
     android:layout_width="match_parent" 
     android:layout_height="300dp" 
     android:layout_centerInParent="true" 
     android:layout_margin="8dp" /> 

答えて

3

あなたが外断片の内部にネストSupportMapFragmentを使用しているので、あなたの代わりにgetFragmentManager()getChildFragmentManager()を使用する必要があります。

SupportMapFragment mapFragment = (SupportMapFragment) getChildFragmentManager() 
    .findFragmentById(R.id.ftMap); 
+0

すべての作品!どうもありがとうございました! – ADR

関連する問題