티스토리 뷰

에러 : java.lang.NullPointerException: CameraUpdateFactory is not initialized


map을 사용하던 중 특정폰에서 "CameraUpdateFactory is not initialized"같은 에러가 발생 되면서 종료되는 이슈가 발생하였습니다.

해당 에러는 map의 CameraUpdateFactory를 이용해 map에 터치 혹은 코드상으로 이동시에 발생되는 이벤트를 처리 하기 위해서 사용 소스코드 인데, 에러 그대로 map의 CameraUpdateFactory가 초기화되지 못했기 때문에 발생 되게 됩니다. 


이를 해결 하기 위해서는 map의 초기화를 진행하는 코드를 넣어 주어야 합니다.


아래의 코드는 Fragment에서 map을 사용할때 예입니다. Activity를 이용하여 맵의 이용 하는 경우 onCreateView()가 아닌 onCreate()에서 처리 해주시면 됩니다.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
@Override public View 
onCreateView(LayoutInflater inflater, ViewGroup container,
             Bundle savedInstanceState)
{
    View view = inflater.inflate(R.layout.map_panel, container, false);
    mapView = (MapView) view.findViewById(R.id.map_view);
    mapView.onCreate(savedInstanceState);
    configureMap(mapView.getMap());
    return view;
}
 
private void 
configureMap(GoogleMap map, double lat, double lon)
{
    if (map == null)
        return// Google Maps not available
    try {
        MapsInitializer.initialize(getActivity());
    }
    catch (GooglePlayServicesNotAvailableException e) {
        Log.e(LOG_TAG, "Have GoogleMap but then error", e);
        return;
    }
    map.setMyLocationEnabled(true);
    LatLng latLng = new LatLng(lat, lon);
    CameraUpdate camera = CameraUpdateFactory.newLatLng(latLng);
    map.animateCamera(camera);
}
 
cs


댓글