Android studio kotlin.TypeCastException:null不能转换为非null类型错误

Android studio kotlin.TypeCastException:null不能转换为非null类型错误,android-studio,kotlin,Android Studio,Kotlin,我正试图得到一个信息框与谷歌地图API的工作。当尝试运行应用程序并添加带有自定义信息框的标记时,它会崩溃。 这是我的密码: 类MainMapsActivity:AppCompatActivity(),OnMapReadyCallback{ private lateinit var mMap: GoogleMap private val test: ArrayList<String> = arrayListOf() private var mLocationPermissionGra

我正试图得到一个信息框与谷歌地图API的工作。当尝试运行应用程序并添加带有自定义信息框的标记时,它会崩溃。 这是我的密码:

类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);


    })
我的问题是如何将此语句更改为非null类型

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

您的
findfragmentById
正在返回
null
(这表示您的片段不存在)

如果您完全确定此时片段不能为
null
,则需要查看您的代码。可能您的片段未附加到
supportFragmentManager

如果您想处理此片段可能不存在的情况,可以使用nullable cast
as?

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

然后,您可以有条件地检查
是否(MapInfo WindowFragment==null)
并处理null情况。

您的
findfragmentById
正在返回
null
(这表示您的片段不存在)

如果您完全确定此时片段不能为
null
,则需要查看您的代码。可能您的片段未附加到
supportFragmentManager

如果您想处理此片段可能不存在的情况,可以使用nullable cast
as?

val mapInfoWindowFragment = supportFragmentManager.findFragmentById(R.id.infoWindowMap) as? MapInfoWindowFragment
然后,您可以有条件地检查
if(mapInfoWindowFragment==null)
并处理null情况