我在學習kotlin,現在對我而言,暫時還沒有一段時間。我有XMLxot onClick中的Kotlin無法正常工作
<ImageView
android:id="@+id/aries"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:tag="1"
android:onClick="clickItemHoro"
android:src="@drawable/aries" />
和片段
class ChooseYourHoroscope : Fragment(){
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?): View? {
val view = inflater.inflate(R.layout.fragment_welcome_old, container, false)
return view;
}
fun clickItemHoro(v: View?){
Log.e("clickItemHoro", v!!.tag.toString())
}
}
當我點擊按鈕,我有錯誤:
Could not find method clickItemHoro(View) in a parent or ancestor Context for android:onClick attribute defined on view class android.support.v7.widget.AppCompatImageView with id 'aries'
爲什麼會發生?代碼變化很簡單,但它不工作,我不明白爲什麼
*科特林在XML中的onClick不行* ......它不會在Java中工作,太... *在父母或祖先上下文* ...是片段的背景? – Selvin
我明白了,坦克! – g71132
[http://stackoverflow.com/questions/14139774/android-app-crashing-fragment-and-xml-onclick](http://stackoverflow.com/questions/14139774/android-app-crashing-fragment-and -xml-onclick)參考這個鏈接說。 – Jacob