1

프래그먼트에서 맵 관리를 얻어야합니다.안드로이드 getMapAsync in Fragment

내 코드

public class Fragment1 extends android.support.v4.app.Fragment implements OnMapReadyCallback{ 

GoogleMap gm; 

private void initializeMap() { 
    if (gm == null) { 
     SupportMapFragment mapFrag = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.mapView); 
     mapFrag.getMapAsync(this); 
    } 
} 

@Override 
public void onMapReady(GoogleMap googleMap) { 
    gm = googleMap; 
    setUpMap(); 
} 

public void setUpMap() 
{ 
    Log.d(LOG_TAG,"Map load!"); 
} 


@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 
    initializeMap(); 
    return inflater.inflate(R.layout.need_help_view, container, false); 
} 

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

}

오류

java.lang.NullPointerException: Attempt to invoke virtual method 'void com.google.android.gms.maps.SupportMapFragment.getMapAsync(com.google.android.gms.maps.OnMapReadyCallback)' on a null object reference 

문제가 무엇인가?

+1

의 사용 가능한 복제 [? NullPointerException이 무엇인가, 나는 그것을 해결 어떻게 (http://stackoverflow.com/questions/218384/what-is- a-null pointerexception-and-how-do-i-fix-it) –

+0

분명히, ID가'R.id.mapView'로 추가 된 프래그먼트가 존재하지 않습니다. – azizbekian

답변

2

보기를 부 풀리지 않고도지도를 초기화하고 있습니다. 따라서 mapFrag은 null입니다.

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 
    initializeMap(); 
    return inflater.inflate(R.layout.need_help_view, container, false); 
} 

이 시도 :

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 
    View v = inflater.inflate(R.layout.need_help_view, container, false); 
    if (gm == null) { 
     SupportMapFragment mapFrag = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.mapView); 
     mapFrag.getMapAsync(this); 
    } 
    return v; 
} 
+0

대단히 감사합니다! –