Kotlin 合成扩展以供查看
Kotlin synthetic extension for view
我有一个包含一些视图的布局,其中一个具有 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 由于接收者类型不匹配
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>
我的错误在哪里?
错误试图告诉您的是,您只能从 Activity
或 Fragment
中访问带有扩展的视图。这是因为它查找具有给定 ID 的视图与您手动执行的操作完全相同,它只是调用 Activity.findViewById()
和 Fragment.getView().findViewById()
,然后将类型转换为特定的View
的子类。我猜你的控制器不是 Activity
或 Fragment
.
如果您能以某种方式将布局的根视图传递给控制器,还有另一种使用扩展的方法。然后您可以执行以下操作:
val title: TextView = rootView.title_whalemare
同样,这只是替换 View.findViewById()
调用和类型转换。
我有一个包含一些视图的布局,其中一个具有 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 由于接收者类型不匹配
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>
我的错误在哪里?
错误试图告诉您的是,您只能从 Activity
或 Fragment
中访问带有扩展的视图。这是因为它查找具有给定 ID 的视图与您手动执行的操作完全相同,它只是调用 Activity.findViewById()
和 Fragment.getView().findViewById()
,然后将类型转换为特定的View
的子类。我猜你的控制器不是 Activity
或 Fragment
.
如果您能以某种方式将布局的根视图传递给控制器,还有另一种使用扩展的方法。然后您可以执行以下操作:
val title: TextView = rootView.title_whalemare
同样,这只是替换 View.findViewById()
调用和类型转换。