android - RadioButton Get ( java.lang.IllegalStateException: selectedRadioButton 不能为 null )
问题描述
所以我在 Kotlin 编程语言中实现了一个简单的 RadioGroup 和 RadioButton 的工作原理,并使用该setOnClickListener
方法使我的应用程序可以像另一个应用程序一样点击,我使用 Fragment 来实现我的简单 RadioGroup 和 RadioButton 应用程序。
当我为此方法传递 SetOnclick 时:
submitButton.setOnClickListener(object : View.OnClickListener {
override fun onClick(v: View?) {
val selectedId = surveyRadioGroup.checkedRadioButtonId
val selectedRadioButton = rootView.findViewById<RadioButton>(selectedId)
Log.d("TEST", selectedRadioButton.text.toString())
dismiss()
}
我有一个错误。
在该代码的上方,这充满了我的代码:
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.RadioButton
import android.widget.RadioGroup
import androidx.fragment.app.DialogFragment
class MyFragment : DialogFragment() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
}
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val rootView: View = inflater.inflate(R.layout.dialog_fragment, container, false)
val cancelButton = rootView.findViewById<Button>(R.id.cancelButton)
var submitButton = rootView.findViewById<Button>(R.id.submitButton)
var surveyRadioGroup = rootView.findViewById<RadioGroup>(R.id.myradiogroup)
cancelButton.setOnClickListener(object : View.OnClickListener {
override fun onClick(v: View?) {
dismiss()
}
})
submitButton.setOnClickListener(object : View.OnClickListener {
override fun onClick(v: View?) {
val selectedId = surveyRadioGroup.checkedRadioButtonId
val selectedRadioButton = rootView.findViewById<RadioButton>(selectedId)
Log.d("TEST", selectedRadioButton.text.toString())
dismiss()
}
})
return rootView
}
}
这是我用于连接 XML 类和 Kotlin 类的 dialog_fragment.xml 上面的代码。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="50dp">
<RadioGroup
android:id="@+id/myradiogroup"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="10dp" />
<RadioButton
android:id="@+id/radioButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="false"
android:text="Exellent"
android:layout_marginTop="30dp"/>
<RadioButton
android:id="@+id/radioButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:checked="false"
android:text="Very GOOD"
tools:ignore="ObsoleteLayoutParam"
android:layout_marginTop="60dp"/>
<RadioButton
android:text="Good"
android:id="@+id/radioButton3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="false"
android:layout_marginTop="90dp"/>
<RadioButton
android:text="Average"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="false"
android:id="@+id/radioButton4"
android:layout_marginTop="120dp"/>
<RadioButton
android:text="Bad"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="false"
android:id="@+id/radioButton5"
android:layout_marginTop="150dp"/>
<Button
android:text="Submit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:checked="false"
android:id="@+id/submitButton"
android:layout_marginTop="200dp"
/>
<Button
android:text="cancel"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:checked="false"
android:id="@+id/cancelButton"
android:layout_marginTop="270dp"
/>
</RelativeLayout>
</RelativeLayout>
此应用为 Androidx 创建了要求
解决方案
selectedRadioButton
为空,因为:
val selectedRadioButton = rootView.findViewById<RadioButton>(selectedId)
在内部调用,onCreateView()
即rootView
此时尚未创建。
onViewCreated()
改为设置您的视图。
推荐阅读
- python - 如何创建仍然将垃圾收集到范围之外的循环引用?
- kubernetes - 使用 Persistent Volume Claim 时是否必须显式创建 Persistent Volume?
- python - Python BeautifulSoup 循环遍历 div 和多个元素
- python-3.x - 列出 Python 多处理中丢失的值
- c# - 我正在尝试获得增值税和总计的最佳计算
- javascript - 如何访问复杂的 JSON 数组和对象数据?
- c++ - 如何在 C++ 的终端中显示图像?
- html - 如何在 IE11 中实现 CSS Grid?
- ruby-on-rails - Sidekiq 计划作业被自动删除(Sidekiq + Rails)
- elasticsearch - 过滤聚合结果