18

我有一些视图的布局,其中一个有 idtitle_whalemare

import kotlinx.android.synthetic.main.controller_settings.*
import kotlinx.android.synthetic.main.view_double_text.*

class MainSettingsController : BaseMvpController<MvpView, MvpPresenter>() {

    val title: TextView = title_whalemare

    override fun getLayout(): Int {
        return R.layout.controller_settings
    }
}

我尝试用 找到它kotlin extensions,但我不能,因为我收到以下错误

由于接收器类型不匹配,以下候选均不适用 由于接收器类型不匹配,以下候选均不适用

controller_settings.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              xmlns:tools="http://schemas.android.com/tools"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:orientation="vertical">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/title_whalemare"/>

</LinearLayout>

我的错误在哪里?

4

1 回答 1

25

该错误试图告诉您的是,您只能从Activitya 或 a中访问带有 Extensions 的视图Fragment。这是因为查找具有给定 ID 的视图的操作与您手动执行的操作完全相同,它只是调用Activity.findViewById()and Fragment.getView().findViewById(),然后将类型转换为View. 我猜你的 Controller 不是Activityor Fragment

如果您可以以某种方式将布局的根视图传递给控制器​​,那么还有另一种使用扩展的方法。然后您可以执行以下操作:

val title: TextView = rootView.title_whalemare

同样,这只是替换View.findViewById()调用和类型转换。

于 2017-04-23T07:38:58.093 回答