Spinner 在 Kotlin 上的 Fragment 中不起作用
Spinner is not working in Fragment on Kotlin
微调器不起作用,我尝试了不同版本的代码,但在其中任何一个都不起作用
谁能帮忙解决这个问题?
TransferFragment.kt
package com.example.hotel2.transfer
class TransferFragment : Fragment(){
private var _binding: FragmentTransferBinding? = null
private val binding get() = _binding!!
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
_binding = FragmentTransferBinding.inflate(inflater, container, false)
val view = binding.root
return view
val transfers = arrayOf<String>("Flowers", "Candies")
val adapter: ArrayAdapter<String> =
ArrayAdapter<String>(activity?.applicationContext!!, android.R.layout.simple_spinner_item, transfers)
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
val spinner = binding.spinner
spinner.adapter = adapter
spinner.prompt = "Title"
spinner.setOnItemSelectedListener(object : AdapterView.OnItemSelectedListener {
override fun onItemSelected(
parent: AdapterView<*>?, view: View?,
position: Int, id: Long
) {
}
override fun onNothingSelected(arg0: AdapterView<*>?) {}
})
}
override fun onDestroyView() {
super.onDestroyView()
_binding = null
}
}
fragment_transfer.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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=".transfer.TransferFragment">
<Spinner
android:id="@+id/spinner"
android:layout_width="200dp"
android:layout_height="wrap_content"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="300dp"
android:layout_marginStart="50dp"></Spinner>
</androidx.constraintlayout.widget.ConstraintLayout>
根据我的猜测,问题出在activity?.applicationContext!!
,但我不知道如何解决。
在 onCreateView()
中,您将此语句作为方法中的第 4 条语句:
return view
这条语句之后的所有代码都不会被执行。
有趣的是,您的 IDE(Android Studio 或其他)应该告诉您!
微调器不起作用,我尝试了不同版本的代码,但在其中任何一个都不起作用
谁能帮忙解决这个问题?
TransferFragment.kt
package com.example.hotel2.transfer
class TransferFragment : Fragment(){
private var _binding: FragmentTransferBinding? = null
private val binding get() = _binding!!
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
_binding = FragmentTransferBinding.inflate(inflater, container, false)
val view = binding.root
return view
val transfers = arrayOf<String>("Flowers", "Candies")
val adapter: ArrayAdapter<String> =
ArrayAdapter<String>(activity?.applicationContext!!, android.R.layout.simple_spinner_item, transfers)
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
val spinner = binding.spinner
spinner.adapter = adapter
spinner.prompt = "Title"
spinner.setOnItemSelectedListener(object : AdapterView.OnItemSelectedListener {
override fun onItemSelected(
parent: AdapterView<*>?, view: View?,
position: Int, id: Long
) {
}
override fun onNothingSelected(arg0: AdapterView<*>?) {}
})
}
override fun onDestroyView() {
super.onDestroyView()
_binding = null
}
}
fragment_transfer.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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=".transfer.TransferFragment">
<Spinner
android:id="@+id/spinner"
android:layout_width="200dp"
android:layout_height="wrap_content"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="300dp"
android:layout_marginStart="50dp"></Spinner>
</androidx.constraintlayout.widget.ConstraintLayout>
根据我的猜测,问题出在activity?.applicationContext!!
,但我不知道如何解决。
在 onCreateView()
中,您将此语句作为方法中的第 4 条语句:
return view
这条语句之后的所有代码都不会被执行。
有趣的是,您的 IDE(Android Studio 或其他)应该告诉您!