Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why google map v2 suddenly crashed by java.lang.NullPointerException: Attempt to get length of null array?

I have an issue that caused by NullPointerException. App starts crashing today when i'm trying to do mMapView.onCreate(savedInstanceState); but yesterday it was fine. I just change my layout file to another (similar) with another name. Here's exception:

E/AndroidRuntime: FATAL EXCEPTION: main Process: com.parking.android, PID: 17585 java.lang.NullPointerException: Attempt to get length of null array at maps.G.a.a(Unknown Source) at maps.V.N.a(Unknown Source) at maps.D.e.a(Unknown Source) at maps.D.p.a(Unknown Source) at maps.ad.ae.a(Unknown Source) at maps.ad.t.a(Unknown Source) at maps.ad.R.a(Unknown Source) at uo.onTransact(:com.google.android.gms.DynamiteModulesB:66) at android.os.Binder.transact(Binder.java:385) at com.google.android.gms.maps.internal.IMapViewDelegate$zza$zza.onCreate(Unknown Source) at com.google.android.gms.maps.MapView$zza.onCreate(Unknown Source) at com.google.android.gms.dynamic.zza$3.zzb(Unknown Source) at com.google.android.gms.dynamic.zza$1.zza(Unknown Source) at com.google.android.gms.maps.MapView$zzb.zzbow(Unknown Source) at com.google.android.gms.maps.MapView$zzb.zza(Unknown Source) at com.google.android.gms.dynamic.zza.zza(Unknown Source) at com.google.android.gms.dynamic.zza.onCreate(Unknown Source) at com.google.android.gms.maps.MapView.onCreate(Unknown Source) at com.parking.android.map.fragment.MapsFragment.initMap(MapsFragment.java:105) at com.parking.android.map.fragment.MapsFragment.onCreateView(MapsFragment.java:92) at com.parking.android.base_arch.fragment.BaseFragment.onCreateView(BaseFragment.java:48) at android.support.v4.app.Fragment.performCreateView(Fragment.java:1974) at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067) at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1252) at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:742) at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1617) at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:517) at android.os.Handler.handleCallback(Handler.java:815) at android.os.Handler.dispatchMessage(Handler.java:104) at android.os.Looper.loop(Looper.java:194) at android.app.ActivityThread.main(ActivityThread.java:5550) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:955) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:750)

And here is some of my code with mapView:

@Override
protected void onCreateView(Bundle savedInstanceState) {
    super.onCreateView(savedInstanceState);
    initMap(savedInstanceState);
}
private void initMap(Bundle savedInstanceState) {
    mMapView.onCreate(savedInstanceState);
    mMapView.onResume();
    try {
        MapsInitializer.initialize(getActContext());
    } catch (Exception e) {
        e.printStackTrace();
    }
    mMapView.getExtendedMapAsync(this);
    locationManager = (LocationManager) getContext().getSystemService(Context.LOCATION_SERVICE);
    try {
        locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 400, 1000, this);
    } catch (SecurityException e) {
        Log.e(TAG, "onLocationChanged: ", e);
    }
}
@Override
public void onPause() {
    mMapView.onPause();
    super.onPause();
}

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

@Override
public void onDestroy() {
    mMapView.onDestroy();
    super.onDestroy();
}
@Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;
    presenter.getParkings();
    mMap.setOnMarkerClickListener(this);
    mMap.setMyLocationEnabled(true);
    mMap.getUiSettings().setMyLocationButtonEnabled(false);

}
@Override
public void onLocationChanged(Location location) {
    if (mMap == null)
        return;
    LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
    CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(latLng, 14);
    mMap.animateCamera(cameraUpdate);
    try {
        locationManager.removeUpdates(this);
    } catch (SecurityException e) {
        Log.e(TAG, "onLocationChanged: ", e);
    }
}
like image 618
Alex Koba Avatar asked Sep 21 '16 18:09

Alex Koba


People also ask

How to fix exception in thread main java lang NullPointerException?

In Java, the java. lang. NullPointerException is thrown when a reference variable is accessed (or de-referenced) and is not pointing to any object. This error can be resolved by using a try-catch block or an if-else condition to check if a reference variable is null before dereferencing it.

How to avoid null pointer exception in java array?

Answer: Some of the best practices to avoid NullPointerException are: Use equals() and equalsIgnoreCase() method with String literal instead of using it on the unknown object that can be null. Use valueOf() instead of toString() ; and both return the same result.


1 Answers

EDIT 2: This bug is solved on newest versions of Google Maps.

EDIT: A user has pointed that this bug is happening also in 7.0 devices, so keep in mind that you may change android:maxSdkVersion="22" to an appropiate Sdk version.

The problem is only happening in 5.1.1 devices with custom roms like Resurrection Remix. I don't know the reason, but you can solve it adding this to your manifest, as seen in https://stackoverflow.com/a/39696819/4208583 and https://stackoverflow.com/a/39724556/4208583.

<uses-permission
        android:name="android.permission.WRITE_EXTERNAL_STORAGE"
        android:maxSdkVersion="22" />
<uses-permission
        android:name="android.permission.READ_EXTERNAL_STORAGE"
        android:maxSdkVersion="22" />
like image 73
Grender Avatar answered Sep 17 '22 21:09

Grender