[英]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
找到它,但我不能,因為我收到以下錯誤
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
訪問帶有 Extensions 的視圖。 這是因為查找具有給定 ID 的視圖的操作與您手動執行的操作完全相同,它只是調用Activity.findViewById()
和Fragment.getView().findViewById()
,然后執行類型強制轉換為View
的特定子類。 我猜您的 Controller 不是Activity
或Fragment
。
如果您可以以某種方式將布局的根視圖傳遞給控制器,那么還有另一種使用擴展的方法。 然后您可以執行以下操作:
val title: TextView = rootView.title_whalemare
同樣,這只是替換View.findViewById()
調用和類型轉換。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.