为什么谷歌地图V2突然崩溃的java.lang.NullPointerException:试图获得空数组的长度?

我有一个由NullPointerException引起的问题。 应用程序今天开始崩溃,当我试图做mMapView.onCreate(savedInstanceState); 但昨天没事。 我只是改变我的布局文件到另一个(类似)与另一个名字。 这里是例外:

E / AndroidRuntime:致命例外:主进程:com.parking.android,PID:17585 java.lang.NullPointerException:尝试获取maps.Gaa(Unknown Source)处的null数组的长度在maps.VNa(Unknown Source) .dea(未知源)在maps.dpa(未知源)在maps.ad.ae.a(未知源)在maps.ad.ta(未知源)在maps.ad.Ra(未知源)在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(未知来源)com.google.android.gms.maps.MapView $ zza.onCreate(未知来源)com.google.android.gms.dynamic.zza $ 3.zzb(未知来源)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 )com.google.android.gms.dynamic.zza.zza(Unknown Source)com.google.android.gms.dynamic.zza.onCreate(Unknown Source)at com.goog le.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 android.support.v4.app.Fragment.performCreateView(Fragment.java:1974)at com.parking.android.base_arch.fragment.BaseFragment.onCreateView(BaseFragment.java:48)。在android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1252)上的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。在android.app.ActivityThread.main(ActivityThread.java)android.os.Looper.loop(Looper.java:194)android.os.Handler.dispatchMessage(Handler.java:104)handleCallback(Handler.java:815) :5550)at java.lang.reflect.Method.invoke(Native Method)at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit .java:955)at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:750)

以下是我的一些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); } } 

编辑2:这个错误是在最新版本的Google地图上解决的。

编辑:用户指出,这个错误也发生在7.0设备,所以请记住,您可以更改android:maxSdkVersion="22"到一个适当的Sdk版本。

这个问题只发生在5.1.1带有自定义ROM的设备上,比如Resurrection Remix。 我不知道原因,但你可以解决它添加到您的清单,如在https://stackoverflow.com/a/39696819/4208583和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" />