-2

SO I'm able to use MapFragment no issue but when I change it to supportMapFragment it bails out. with the below error. not sure what I'm doing wrong that one works and the other doesn't. The reason I need the support one is that I can't remove the fragment with when using mapfragment because the below line is expectin Fragment! and is receiving MapFragment.....so it fails...when I switch it to SupportMapFragment the below line works but the errors happen below.

Thanks

    getFragmentManager().beginTransaction().remove(mapFragment).commit();

XML

 <fragment
            android:id="@+id/mapF"
            class="com.google.android.gms.maps.SupportMapFragment"
            android:layout_width="match_parent"
            android:layout_height="200dp" />

CLASS

import android.location.Address
import android.os.Bundle
import android.support.v4.app.Fragment
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.google.android.gms.maps.*
import com.google.android.gms.maps.model.LatLng
import com.google.android.gms.maps.model.MarkerOptions
import android.location.Geocoder
import android.text.Html
import android.webkit.WebView
import android.widget.Toast
import com.beust.klaxon.JsonObject
import com.beust.klaxon.Parser
import com.beust.klaxon.obj
import com.beust.klaxon.string
import com.github.kittinunf.fuel.httpGet
import com.github.kittinunf.fuel.httpPost
import com.github.kittinunf.result.Result
import com.github.kittinunf.result.getAs
import kotlinx.android.synthetic.main.fragment_1.view.*
import kotlinx.android.synthetic.main.fragment_event_details.*
import kotlinx.android.synthetic.main.fragment_event_details.view.*
import java.util.*
import kotlin.collections.ArrayList



class Event_details : Fragment(),OnMapReadyCallback{
    lateinit var mMap:GoogleMap
    lateinit var mWeb:WebView
    lateinit var mapFragment:SupportMapFragment

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)


    }



    override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?,
                              savedInstanceState: Bundle?): View? {
        var myView = inflater!!.inflate(R.layout.fragment_event_details, container, false)

        var mP = Pair("news_id",101)
        var mL:ArrayList<Pair<String,Int>> = ArrayList<Pair<String,Int>>()
        mL.add(mP)


       ****do some JSON STUFF****


        >>>>> LINNE 125   mapFragment = activity.supportFragmentManager.findFragmentById(R.id.mapF) as SupportMapFragment
        mapFragment.getMapAsync(this) 



        return  myView
    }

ERROR

ntime: FATAL EXCEPTION: main Process: com.example.herbstzu.listview, PID: 11988 kotlin.TypeCastException: null cannot be cast to non-null type com.google.android.gms.maps.SupportMapFragment at com.example.sadfasdf.listview.Event_details.onCreateView(Event_details.kt:125) at android.support.v4.app.Fragment.performCreateView(Fragment.java:2354) at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1419)

SOLUTION

So...the solution was two fold....

When changing from MapFragment to SupportMapFragment.......

  1. Change XML class to SupportMapFragment in side of a fragment
  2. Change so that SupportMapFragment is a nullable throughout your code
  3. Change supportFragmentManager to childFragmentManager..supportFragmentManager will also work however it will not fire the onMapReady...
  4. Smile because you beat down Kotlin/Android like a little ..... ;)
BostonMacOSX
  • 1,369
  • 2
  • 17
  • 38

2 Answers2

0

In kotlin nullable class ( with ? at the end of class type) is different class from non-nullable. findFragmentById can return null so return type is nullable. You are trying to cast in to non-nullable SupportMapFragment.

Try to do it this way activity.supportFragmentManager.findFragmentById(R.id.mapF) as SupportMapFragment?

Karol Kulbaka
  • 1,136
  • 11
  • 21
  • The MapFragment Wasn't null so the supportMapFragment shouldn't be null as well... – BostonMacOSX Oct 14 '17 at 15:04
  • This is from find by id doc `@return The fragment if found or null otherwise.` It is not about it **is** null or not but aboutt **can be**. I've update answer to be more clearly. – Karol Kulbaka Oct 14 '17 at 17:54
0

Since your SupportFragment may be null you need to do a null check. There are several ways to do that. The simplest is

activity.supportFragmentManager.findFragmentById(R.id.mapF)
(mapFragment as SupportFragment)?.getMapAsync(this) 

This will ignore everything after ?. if the value is null.

Using

activity.supportFragmentManager.findFragmentById(R.id.mapF)?.let {
    (it as SupportFragment).getMapAsync(this)
}  

works the same way, except that everything in it is your SupportFragment which cant be null.

The last but not least is to force a nullable value by using

activity.supportFragmentManager.findFragmentById(R.id.mapF)
(mapFragment as YourFragment).!!getMapAsync(this) 

which is not recommended due nullpointerexceptions.

Emanuel
  • 8,027
  • 2
  • 37
  • 56