這個問題在這里已經有了答案: 當我在 Kotlin 中輸入視圖名稱/ID 時,為什么會出現“未決議的參考”錯誤? (2 個回答) 18 小時前關閉。
當我嘗試在我的 activity_main.xml 中使用 TextView 時,我在 MainActivity.kt 中得到一個“未決議的參考”(甚至在嘗試構建之前)?我只是看不出我做錯了什么!
我在 MainActivity 的末尾突出顯示了錯誤。
任何幫助表示贊賞。
主活動.kt
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val carouselRecyclerview = findViewById<CarouselRecyclerview>(R.id.recycler)
val list = ArrayList<DataModel>()
list.add(DataModel(R.drawable.ad_astra, "Adastra"))
list.add(DataModel(R.drawable.beach_bum, "Beach Bum"))
list.add(DataModel(R.drawable.dark_phoenix, "Dark Phoenix"))
list.add(DataModel(R.drawable.glass, "Glass"))
val adapter = DataAdapter(list)
carouselRecyclerview.adapter = adapter
carouselRecyclerview.set3DItem(true)
carouselRecyclerview.setAlpha(true)
val carouselLayoutManager = carouselRecyclerview.getCarouselLayoutManager()
val currentlyCenterPosition = carouselRecyclerview.getSelectedPosition()
carouselRecyclerview.setItemSelectListener(object : CarouselLayoutManager.OnSelected {
override fun onItemSelected(position: Int) {
//Cente item
Toast.makeText(this@MainActivity, list[position].text, Toast.LENGTH_LONG).show()
ShowMeIt.text = "Why does cause a Unresolved reference?"
ShowMeIt.text = list[position].text
}})
}
}
活動_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<com.jackandphantom.carouselrecyclerview.CarouselRecyclerview
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@ id/recycler"/>
<TextView
android:id="@ id/ShowMeIt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="186dp"
android:layout_marginRight="186dp"
android:layout_marginBottom="106dp"
android:text="Hello" />
</RelativeLayout>
uj5u.com熱心網友回復:
我認為你需要像這樣在 MainActivity.kt 上定義
val ShowMeIt= findViewById<TextView>(R.id.ShowMeIt)
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/388567.html
上一篇:添加表時房間遷移失敗
