本文介绍了为什么谷歌地图V2突然崩溃的java.lang.NullPointerException:试图获得空数组的长度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

lockquote
E / AndroidRuntime:致命例外:main
过程:com.parking.android,PID :17585
java.lang.NullPointerException:尝试获取maps.Gaa(未知源)的空数组
的长度
在地图上的
。 Dea(未知源)
在maps.Dpa(未知源)
在maps.ad.ae.a(未知源)
在maps.ad.ta(未知源)
在maps.ad.Ra(未知源)
在uo.onTransact(:com.google.android.gms.DynamiteModulesB:66)
在android.os.Binder.transact(Binder.java:385 )
at
com.google.android.gms.maps.internal.IMapViewDelegate $ zza $ zza.onCreate(未知
来源)
at 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(Unknown Source)
at
com.parking.android.map.fragment.MapsFragment.initMap(MapsFragment.java:105)
at
com。 parking.android.map.fragment.MapsFragment.onCreateView(MapsFragment.ja va: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(F ragmentManager.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)$ b $在java.lang.reflect.Method.invoke(本地方法)$ b $在java.lang.reflect.Method.invoke(Method.java:372)

com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:955)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:750) p>

以下是我的一些mapView代码:

  @Override 
protected void onCreateView(Bundle savedInstanceState){
super.onCreateView(savedInstanceState);
initMap(savedInstanceState);
}
private void initMap(Bundle savedInstanceState){
mMapView.onCreate(savedInstanceState);
mMapView.onResume();
尝试{
MapsInitializer.initialize(getActContext());
} catch(Exception e){
e.printStackTrace();
}
mMapView.getExtendedMapAsync(this);
locationManager =(LocationManager)getContext()。getSystemService(Context.LOCATION_SERVICE);
尝试{
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);

$ b @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);
尝试{
locationManager.removeUpdates(this);
} catch(SecurityException e){
Log.e(TAG,onLocationChanged:,e);
}
}


解决方案


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


这个问题只发生在5.1.1带有自定义ROM的设备上,比如Resurrection Remix。我不知道原因,但是您可以将其添加到清单中,如和。 p>

 < uses-permission 
android:name =android.permission.WRITE_EXTERNAL_STORAGE
android:maxSdkVersion = 22/>
< uses-permission
android:name =android.permission.READ_EXTERNAL_STORAGE
android:maxSdkVersion =22/>


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:

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);
    }
}
解决方案

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" />

这篇关于为什么谷歌地图V2突然崩溃的java.lang.NullPointerException:试图获得空数组的长度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-03 10:02