【问题标题】:RadioButton Get ( java.lang.IllegalStateException: selectedRadioButton must not be null )RadioButton Get ( java.lang.IllegalStateException: selectedRadioButton 不能为 null )
【发布时间】:2021-02-01 02:51:41
【问题描述】:

所以我在 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 创建了需求

【问题讨论】:

    标签: android xml kotlin radio-button radio-group


    【解决方案1】:

    selectedRadioButton 为空,因为:

    val selectedRadioButton = rootView.findViewById<RadioButton>(selectedId)
    

    onCreateView() 内部调用,即 rootView 此时尚未创建。

    改为在onViewCreated() 中设置您的视图。

    【讨论】:

    • 能否请您提供有关 onViewCreated() 的完整代码。
    • stackoverflow 的理念是回答问题而不是提供可以复制和粘贴的代码。
    • 如果您想了解有关 onViewCreated() 的信息,可以查看 android 开发者文档。了解这些生命周期方法很重要。
    • 好的,谢谢你提醒我有关复制粘贴的事情,非常感谢。
    • 如果这解决了你的问题,请投票给这个答案。
    猜你喜欢
    • 2021-02-01
    • 2018-05-04
    • 2022-01-09
    • 1970-01-01
    • 2015-11-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多