Kotlin与Android中的地图 [英] Kotlin with Map in Android

查看:98
本文介绍了Kotlin与Android中的地图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?, savedInstanceState: Bundle?): View {
var view: View = inflater?.inflate(R.layout.map_fragment, null)!!

var mapFragment : SupportMapFragment?=null
mapFragment= fragmentManager.findFragmentById(R.id.map) as SupportMapFragment
mapFragment.getMapAsync(this)

return view
}

Logcat:

FATAL EXCEPTION: main
kotlin.TypeCastException: null cannot be cast to non-null type 
com.google.android.gms.maps.SupportMapFragment
at example.com.kotlinexamplebydimple.Mapfragment.onCreateView(Mapfragment.kt:36)

在此行上显示错误:

mapFragment= fragmentManager.findFragmentById(R.id.map) as SupportMapFragment

推荐答案

您已声明mapFragment可为空,因此您必须对其进行处理:

You declared mapFragment to be nullable so you have to deal with it:

var mapFragment : SupportMapFragment?=null
mapFragment = fragmentManager.findFragmentById(R.id.map) as SupportMapFragment?
mapFragment?.getMapAsync(this)

这篇关于Kotlin与Android中的地图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆