kotlin.TypeCastException:无法将null强制转换为非null类型错误

时间:2018-06-27 17:48:58

标签: android-studio kotlin

我正在尝试使用Google Maps 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);


    })

这是Logcat给我的错误:

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)

我的问题是如何将该语句更改为非null类型?

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

1 个答案:

答案 0 :(得分:3)

您的findfragmentById返回null(表明您的片段不存在)。

如果您现在完全确定片段不能为null,则需要检查代码。也许您的片段未附加到supportFragmentManager

如果要处理此片段可能不存在的情况,则可以使用可为空的强制转换as?

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

然后,您可以有条件地检查if(mapInfoWindowFragment == null)并处理空值情况。

相关问题