I have a library written by myself in which I have been using Google Maps Android SDK version 18.1.0 earlier(recently upgraded to 18.2,0). I have two Android Apps with me, let’s call them app1 and app2. In app1, I use MapFragment which I have hosted in the library, and I have positioned the “my current location” button to the bottom right corner of the screen with custom padding all in the library. I do this by accessing the myPosition button as follows:
val myLocationButton = (mapFragment.view?.findViewById<View>(Integer.parseInt("1"))?.parent as View).findViewById<View>(Integer.parseInt("2"))
I recently updated the Google Maps Android SDK to the latest version (v18.2.0) in the library Gradle file, ran app1 and tried to open the activity which is hosting MapFragment. The app crashed with the following error message:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.myProject.myActivity/com.myApp.main.ui.myAppMapActivity}: java.lang.NullPointerException: null cannot be cast to non-null type android.view.View
This app is not recreated second time. It only occurs when I reinstall again and try access the activity hosting MapFragment.
In order to inspect the view hierarchy of MapFragment in both versions(v18.1.0 and v18.2.0), I printed logs, wherein I printed the view type and its viewId. I noticed that in the earlier version, the ImageView(the my location button(mapFragment.view?.findViewById<View>(Integer.parseInt("1"))
) has a view ID of 1. In the latest version, it is having a view ID of 3.
Hence I tried changing the code to as follows, and it worked for me:
val myLocationButton = (mapFragment.view?.findViewById<View>(Integer.parseInt("3"))?.parent as View).findViewById<View>(Integer.parseInt("4"))
While this works for me in app1, I am unable to reproduce the same crash in app2. I want to understand why the crash is happening only in app1 and not in app2, when I am using the same activity from my library.
codecode42 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.