0

I'm trying to get a info box working with Google Maps API. When trying to run the application, and adding a marker with custom info box it crashes. Here is my code:

class MainMapsActivity : AppCompatActivity(), OnMapReadyCallback {

private lateinit var mMap: GoogleMap
private val test: ArrayList<String> = arrayListOf()

private var mLocationPermissionGranted: Boolean = false
private val PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION: Int = 1234
// ^Number isnt definitive, as long as its unique inside the application
private val DEFAULT_ZOOM: Float = 15.0F

private var mLastKnownLocation: Location? = null

private val mDefaultLocation = LatLng(60.312491, 24.484248)

private lateinit var mFusedLocationProviderClient: FusedLocationProviderClient





override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main_maps)


    // Obtain the SupportMapFragment and get notified when the map is ready to be used.
    val mapFragment = supportFragmentManager
            .findFragmentById(R.id.map) as SupportMapFragment
    mapFragment.getMapAsync(this)

    mFusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(this)
}


override fun onMapReady(googleMap: GoogleMap) {
    mMap = googleMap

    getDeviceLocation()

    //PRESSING WILL ADD MARKER
    mMap.setOnMapClickListener(GoogleMap.OnMapClickListener { point ->
        val builder: AlertDialog.Builder
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            builder = AlertDialog.Builder(this, android.R.style.Theme_Material_Dialog_Alert)
        } else {
            builder = AlertDialog.Builder(this)
        }
        val marker = MarkerOptions().position(point)
        builder.setTitle("Are you sure you want to add a map location here?")
                .setMessage("Are you sure you want to add a map location here?")
                .setPositiveButton(android.R.string.yes, DialogInterface.OnClickListener { dialog, which ->
                    mMap.addMarker(marker)
                            //CUSTOM MARKER
                            .setIcon(BitmapDescriptorFactory.fromResource(R.mipmap.pinetree_foreground))
                })

                .setNegativeButton(android.R.string.no, DialogInterface.OnClickListener { dialog, which ->
                    // do nothing
                })
                .show()

        true
       val mapInfoWindowFragment = supportFragmentManager.findFragmentById(R.id.infoWindowMap) as MapInfoWindowFragment

        //Set Custom InfoWindow
         val infoWindow = InfoWindow(point, InfoWindow.MarkerSpecification(0, 0), mapInfoWindowFragment)
        // Shows the InfoWindow or hides it if it is already opened.
        mapInfoWindowFragment.infoWindowManager()?.toggle(infoWindow, true);


    })

And here is the error Logcat gives me:

kotlin.TypeCastException: null cannot be cast to non-null type com.example.sampo.luontoalueet.MapInfoWindowFragment
    at com.example.sampo.luontoalueet.MainMapsActivity$onMapReady$1.onMapClick(MainMapsActivity.kt:123)

My question is how to change change this statement into non-null type?

val mapInfoWindowFragment = supportFragmentManager.findFragmentById(R.id.infoWindowMap) as MapInfoWindowFragment

1 Answer 1

3

Your findfragmentById is returning null (which indicates that your fragment does not exist).

If you're absolutely sure that fragment cannot be null at this point, you need to review your code. Maybe your fragment is not attached to supportFragmentManager?

If you want to handle a case where this fragment might not exist, You can use nullable cast as?:

val mapInfoWindowFragment = supportFragmentManager.findFragmentById(R.id.infoWindowMap) as? MapInfoWindowFragment

Then you can check conditionally if(mapInfoWindowFragment == null) and handle the null case.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.