2017-04-23 58 views
5

我有一些意见的布局,其中一人有ID title_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找到它,但我不能,因为我得到以下错误

无以下候选人的是因为接收器类型不匹配的适用 None of the following candidates is applicable because of receiver type mismatch

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> 

我的错误在哪里?

回答

6

该错误试图告诉您的是,您只能从ActivityFragment内使用扩展访问视图。这是因为它完全一样,可以通过手动方式查找具有给定ID的视图,只需调用Activity.findViewById()Fragment.getView().findViewById(),然后将类型转换为View的特定子类。我猜你的控制器不是ActivityFragment

如果您可以以某种方式将布局的根视图传递给控制器​​,还有另一种使用扩展的方式。然后,你可以做到以下几点:

val title: TextView = rootView.title_whalemare 

再次,这是刚刚更换View.findViewById()调用和类型转换。