android.view.ContextThemeWrapper cannot be cast to android.app.Activity

后端 未结 2 1479
天命终不由人
天命终不由人 2021-02-20 05:28

I\'m not a designer but when i got this project i can`t open specifically some screens, i think that they are screens and we only reuse some of the layouts have been created. An

2条回答
  •  情歌与酒
    2021-02-20 06:20

    Recursive solution in Kotlin:

    fun Context.getActivity(): Activity? {
        return when (this) {
            is Activity -> this
            is ContextWrapper -> this.baseContext.getActivity()
            else -> null
        }
    }
    

    Checked with case of using View.getContext().

提交回复
热议问题