java.lang.NullPointerException: CameraUpdateFactory is not initialized logcat exception

自闭症网瘾萝莉.ら 提交于 2019-12-19 05:34:10

问题


I'm working with Google Maps. I am able to successfully create and can show a Google Map. Now I want to add CameraUpdate(latitude, longitude). I googled and I found some source code but I'm getting a NullPointerException.

The LogCat error message is:

java.lang.NullPointerException: CameraUpdateFactory is not initialized

This is my source. What am I doing wrong?

public class StradaContact extends Fragment {

public final static String TAG = StradaContact.class.getSimpleName();
private MapView mMapView;
private GoogleMap mMap;
private Bundle mBundle;
double longitude = 44.79299800000001, latitude = 41.709981;

public StradaContact() {

}

public static StradaContact newInstance() {
    return new StradaContact();
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.strada_contact, container,false);

    mMapView = (MapView) rootView.findViewById(R.id.pointMap);

    mMapView.onCreate(mBundle);
    setUpMapIfNeeded(rootView);

    return rootView;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mBundle = savedInstanceState;
}

private void setUpMapIfNeeded(View inflatedView) {
    if (mMap == null) {
        mMap = ((MapView) inflatedView.findViewById(R.id.pointMap)).getMap();
        if (mMap != null) {
            CameraUpdate center = CameraUpdateFactory.newLatLng(new LatLng(latitude, longitude));
            CameraUpdate zoom = CameraUpdateFactory.zoomTo(15);
            mMap.moveCamera(center);
            mMap.animateCamera(zoom);
        }
    }
}

@Override
public void onResume() {
    super.onResume();
    mMapView.onResume();
}

@Override
public void onPause() {
    super.onPause();
    mMapView.onPause();
}

@Override
public void onDestroy() {
    mMapView.onDestroy();
    super.onDestroy();
}

}

<com.google.android.gms.maps.MapView
        android:id="@+id/pointMap"
        android:name="com.google.android.gms.maps.MapFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"

        class="com.google.android.gms.maps.MapFragment" />

回答1:


Try this in "onCreate":

try {
 MapsInitializer.initialize(this);
} catch (GooglePlayServicesNotAvailableException e) {
 e.printStackTrace();
}



回答2:


Try with following method initialize google map api before using

MapsInitializer.initialize(getActivity());


来源:https://stackoverflow.com/questions/24448378/java-lang-nullpointerexception-cameraupdatefactory-is-not-initialized-logcat-ex

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!