MapView onCreate在LG设备上抛出NullPointerException

时间:2017-06-19 06:51:21

标签: android google-maps

首先,这个问题对我来说只能在运行Android 6的LG设备(LG G4和G5)上重现。问题是通过谷歌播放服务的上次更新引入的。如果我手动降级LG设备上的Google Play服务,则错误消失了。不幸的是我不能告诉我的用户这样做,所以我需要找到另一个解决方案。由于此错误可以在我的应用程序在Play商店中重现,我可以确定错误不是通过我自己的代码更改引入的。

通过调用MapView中的onCreate()方法发生错误。我将MapView子类化,并拥有自己的类

public class MyMap extends MapView implements OnMapReadyCallback {
    ...
}

在MyMap的构造函数中,我调用了onCreate方法

public MyMap() {
    ...
    this.onCreate(null);
    this.onResume();
    this.getMapAsync(this);
    ...
}

在运行Android 6的LG设备上,会抛出以下异常:

java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources com.google.maps.api.android.lib6.impl.aq.e()' on a null object reference
at com.google.maps.api.android.lib6.gmm6.vector.ah.getResources(:com.google.android.gms.DynamiteModulesB:340)
at android.util.ResolutionOverride.<init>(ResolutionOverride.java:56)
at android.view.SurfaceView.init(SurfaceView.java:207)
at android.view.SurfaceView.<init>(SurfaceView.java:187)
at com.google.maps.api.android.lib6.gmm6.vector.am.<init>(:com.google.android.gms.DynamiteModulesB:1)
at com.google.maps.api.android.lib6.gmm6.vector.ah.<init>(:com.google.android.gms.DynamiteModulesB:3)
at com.google.maps.api.android.lib6.gmm6.api.am.<init>(:com.google.android.gms.DynamiteModulesB:53)
at com.google.maps.api.android.lib6.gmm6.api.am.a(:com.google.android.gms.DynamiteModulesB:49)
at com.google.android.gms.maps.internal.bv.a(:com.google.android.gms.DynamiteModulesB:38)
at com.google.maps.api.android.lib6.impl.az.a(:com.google.android.gms.DynamiteModulesB:80)
at com.google.maps.api.android.lib6.impl.az.a(:com.google.android.gms.DynamiteModulesB:1)
at com.google.maps.api.android.lib6.impl.cw.a(:com.google.android.gms.DynamiteModulesB:18)
at com.google.android.gms.maps.internal.t.onTransact(:com.google.android.gms.DynamiteModulesB:17)
at android.os.Binder.transact(Binder.java:387)
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.zzJy(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)

我发现Airbnb的原生地图有类似的问题,但他们的解决方案似乎与我的问题无关。 https://github.com/airbnb/react-native-maps/issues/1358

我可以尝试任何帮助或其他想法。

1 个答案:

答案 0 :(得分:1)

您可以尝试使用MapFragment而不是MapView,因为更新的Google Play服务11.0.55不支持MapView。我使用MapView为我工作。

活动类:

    private SupportMapFragment mMapFragment; 

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        //return inflater.inflate(R.layout.fragment_find_direction, container, false);
        View view = inflater.inflate(R.layout.fragment_find_direction, null, false);
        SupportMapFragment mapFragment = (SupportMapFragment) this.getChildFragmentManager()
                .findFragmentById(R.id.map_fragment);
        mMapFragment.getMapAsync(this);
        mMapFragment = mapFragment;
        return view;
    }

@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
mMapFragment.onCreate(savedInstanceState);
}

Xml类:

<fragment
    class="com.google.android.gms.maps.SupportMapFragment"
    android:id="@+id/map_fragment"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/main_lay"
    android:apiKey="@string/google_map_key"
    android:clickable="true"
    android:enabled="true"
    android:focusable="true"
    app:layout_collapseMode="parallax">
</fragment>