我一直在维护一个使用MapView的应用程序,最近我在尝试调用MapView的onCreate()方法时遇到了NullPointerExceptions的问题。奇怪的是,这仅发生在较新版本的Google Play服务上。当我将版本恢复为出厂默认设置时,MapView可以正常工作。也就是说,适用于9.8.79,不适用于14.3.66。不能在所有生产型手持设备上更改Play服务的版本。
我尝试过的事情
这里有几个标题相似的问题,所以我想现在就抢先使用[重复]标签。我见过的每个其他问题在堆栈跟踪中都有不同的详细信息,建议的答案要么不适用,要么不适用于我的问题。
LogCat
10-11 10:36:59.644 2386-2386/net.xconsulting.fairtrak E/DynamiteLoaderImpl: Error creating module context: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.contains(java.lang.CharSequence)' on a null object reference
10-11 10:36:59.680 2386-2386/net.xconsulting.fairtrak E/AndroidRuntime: FATAL EXCEPTION: main
Process: net.xconsulting.fairtrak, PID: 2386
java.lang.RuntimeException: Unable to start activity ComponentInfo{net.xconsulting.fairtrak/net.xconsulting.fairtrak.activity.NotesActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.contains(java.lang.CharSequence)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2547)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2613)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1469)
at android.os.Handler.dispatchMessage(Handler.java:111)
at android.os.Looper.loop(Looper.java:207)
at android.app.ActivityThread.main(ActivityThread.java:5692)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:908)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:769)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.contains(java.lang.CharSequence)' on a null object reference
at android.content.ContentResolver.checkLeakDetectIgnoreList(ContentResolver.java:2695)
at android.content.ContentResolver.openTypedAssetFileDescriptor(ContentResolver.java:1178)
at android.content.ContentResolver.openAssetFileDescriptor(ContentResolver.java:958)
at android.content.ContentResolver.openFileDescriptor(ContentResolver.java:811)
at android.content.ContentResolver.openFileDescriptor(ContentResolver.java:765)
at tns.a(:com.google.android.gms@14366020@14.3.66 (040406-213742215):33)
at tns.a(:com.google.android.gms@14366020@14.3.66 (040406-213742215):11)
at com.google.android.gms.chimera.container.DynamiteLoaderImpl.createModuleContextNoCrashUtils(:com.google.android.gms@14366020@14.3.66 (040406-213742215):11)
at com.google.android.gms.dynamite.DynamiteModule.b(:com.google.android.gms@14366020@14.3.66 (040406-213742215):12)
at com.google.android.gms.dynamite.DynamiteModule.a(:com.google.android.gms@14366020@14.3.66 (040406-213742215):53)
at com.google.android.gms.dynamite.DynamiteModule.a(:com.google.android.gms@14366020@14.3.66 (040406-213742215):76)
at oky.a(:com.google.android.gms@14366020@14.3.66 (040406-213742215):1)
at com.google.android.gms.maps.internal.CreatorImpl.a(:com.google.android.gms@14366020@14.3.66 (040406-213742215):8)
at com.google.android.gms.maps.internal.CreatorImpl.newMapViewDelegate(:com.google.android.gms@14366020@14.3.66 (040406-213742215):1)
at actl.a(:com.google.android.gms@14366020@14.3.66 (040406-213742215):21)
at cje.onTransact(:com.google.android.gms@14366020@14.3.66 (040406-213742215):3)
at android.os.Binder.transact(Binder.java:392)
at com.google.android.gms.maps.internal.zzc$zza$zza.zza(Unknown Source)
at com.google.android.gms.maps.MapView$zzb.zzzW(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 net.xconsulting.fairtrak.activity.NotesActivity.onCreate(NotesActivity.java:70)
at android.app.Activity.performCreate(Activity.java:6270)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1113)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2500)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2613)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1469)
at android.os.Handler.dispatchMessage(Handler.java:111)
at android.os.Looper.loop(Looper.java:207)
at android.app.ActivityThread.main(ActivityThread.java:5692)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:908)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:769)
代码
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_notes);
MapView mapView = findViewById(R.id.noteMap);
mapView.getMapAsync(this);
mapView.onCreate(savedInstanceState);
}
布局
<com.google.android.gms.maps.MapView
android:id="@+id/noteMap"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
等级依赖性
dependencies {
implementation 'com.google.android.gms:play-services-maps:8.4.0'
//also tried implementation 'com.google.android.gms:play-services-maps:16.0.0'
}