为什么google map v2突然崩溃了java.lang.NullPointerException:尝试获取null数组的长度?

时间:2016-09-21 18:06:13

标签: java android google-maps

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

  

E / AndroidRuntime:致命异常:主要        过程:com.parking.android,PID:17585                                                                        java.lang.NullPointerException:尝试获取null数组的长度                                                                            at maps.G.a.a(未知来源)                                                                            at maps.V.N.a(未知来源)                                                                            at maps.D.e.a(未知来源)                                                                            at maps.D.p.a(未知来源)                                                                            at maps.ad.ae.a(未知来源)                                                                            at maps.ad.t.a(未知来源)                                                                            at maps.ad.R.a(未知来源)                                                                            at uo.onTransact(:com.google.android.gms.DynamiteModulesB:66)                                                                            在android.os.Binder.transact(Binder.java:385)                                                                            在   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(未知来源)                                                                            在com.google.android.gms.maps.MapView $ zzb.zzbow(未知来源)                                                                            在com.google.android.gms.maps.MapView $ zzb.zza(未知来源)                                                                            在com.google.android.gms.dynamic.zza.zza(未知来源)                                                                            在com.google.android.gms.dynamic.zza.onCreate(未知来源)                                                                            在com.google.android.gms.maps.MapView.onCreate(未知来源)                                                                            在   com.parking.android.map.fragment.MapsFragment.initMap(MapsFragment.java:105)                                                                            在   com.parking.android.map.fragment.MapsFragment.onCreateView(MapsFragment.java:92)                                                                            在   com.parking.android.base_arch.fragment.BaseFragment.onCreateView(BaseFragment.java:48)                                                                            在   android.support.v4.app.Fragment.performCreateView(Fragment.java:1974)                                                                            在   android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067)                                                                            在   android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1252)                                                                            在   android.support.v4.app.BackStackRecord.run(BackStackRecord.java:742)                                                                            在   android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1617)                                                                            在   android.support.v4.app.FragmentManagerImpl $ 1.run(FragmentManager.java:517)                                                                            在android.os.Handler.handleCallback(Handler.java:815)                                                                            在android.os.Handler.dispatchMessage(Handler.java:104)                                                                            在android.os.Looper.loop(Looper.java:194)                                                                            在android.app.ActivityThread.main(ActivityThread.java:5550)                                                                            at java.lang.reflect.Method.invoke(Native Method)                                                                            在java.lang.reflect.Method.invoke(Method.java:372)                                                                            在   com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:955)                                                                            在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);
    }
}

3 个答案:

答案 0 :(得分:7)

编辑2:此错误已在最新版本的Google地图上解决。

编辑:用户已指出此错误也发生在7.0设备中,因此请注意,您可以将android:maxSdkVersion="22"更改为适当的Sdk版本。

问题只出现在5.1.1设备中,其中包含Resurrection Remix等自定义roms。我不知道原因,但您可以解决此问题,将其添加到您的清单中,如https://stackoverflow.com/a/39696819/4208583https://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" />

答案 1 :(得分:1)

这是Maps V2中的已知问题

Pre-Populate HTML form file input

答案 2 :(得分:0)

我解决了调用mapView.on~() withint每个生命周期方法 MapView 实现的问题。我没有在onPause()期间调用它,但在那之后,它起作用了。我也在onRestoreInstanceState()中调用它。