arthur3486 / bottomsheets

Material Bottom Sheets library for Android
Apache License 2.0
80 stars 12 forks source link

Error implementing the example #2

Closed Morteza-Rastgoo closed 5 years ago

Morteza-Rastgoo commented 5 years ago

Trying to implement the example in the readme,

class MainActivity : AppCompatActivity() {

    private var bottomSheet : BottomSheet? = null

    private fun showActionsBottomSheet() {
        bottomSheet = ActionPickerBottomSheet.init(
            this,
            getActionOptions().map(::ActionItem),
            Config.Builder(this).build()
        )
        bottomSheet.setOnItemSelectedListener { 
            // do something...
        }
        bottomSheet.show()
    }

    fun getActionOptions() : List<Option> {
        // your options
    }

}

But the error appears: Smart cast to 'ActionPickerBottomSheet' is impossible, because 'bottomSheet' is a mutable property that could have been changed by this time

arthur3486 commented 5 years ago

Hi @Morteza-Rastgoo !

Sorry for the late response!

Yes, indeed, there was a mistake in the README; will correct it shortly.

Regarding your problem, the bottomSheet variable is nullable, which means that in order for you to be able to operate on it you need to perform a null check first (-> bottomSheet?.show()).