简体   繁体   English

为什么我在使用微调器的片段中不断收到 null 参考错误?

[英]Why do I keep getting a null reference error in my fragment with a spinner?

I am trying to put a spinner in my android fragment, but when ever I open the fragment, I get a java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Spinner.setAdapter(android.widget.SpinnerAdapter)' on a null object reference. I am trying to put a spinner in my android fragment, but when ever I open the fragment, I get a java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Spinner.setAdapter(android.widget.SpinnerAdapter) ' 在 null object 参考上。 I tried putting the same code in main activity, and it works.我尝试将相同的代码放在主要活动中,它可以工作。 Is there something wrong with a context?上下文有问题吗?

 class AlarmLabelDialogFragment: DialogFragment() {

    val alarmRepository = AlarmRepository.get()

    lateinit var alarm: Alarm
    private val TAG = "tag1"



    override fun onCreateView(

        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        dialog?.window?.setBackgroundDrawable(R.drawable.ic_launcher_background.toDrawable())
            var rootView: View = inflater.inflate(R.layout.alarm_label_dialog_fragment, container, false)


        val tonesArray = arrayOf("Geese", "Thunder")
        ArrayAdapter.createFromResource(
            requireActivity().applicationContext,
            R.array.tonesArray,
            android.R.layout.simple_spinner_item
        ).also { adapter ->            
            adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
            tonePickerDropDown.adapter = adapter
        }
        //val toneArrayAdapter =  ArrayAdapter(rootView.context, android.R.layout.simple_spinner_item, tonesArray)
        //tonePickerDropDown.adapter = toneArrayAdapter
        val bundle = arguments
        val id = bundle!!.get("id")
        alarm = alarmRepository.getAlarm(id.toString().toLong())

        rootView.alarmLabelEditTextTextMultiLine.setText(alarm.alarmLabel)
        rootView.alarmTimeFragmentLabel.setText(alarm.hour + ":" + alarm.minute)



        rootView.cancelButton.setOnClickListener {
        dismiss()
        }

        rootView.clearButton.setOnClickListener {
            rootView.alarmLabelEditTextTextMultiLine.setText("")
        }

        tonePickerDropDown.setOnItemSelectedListener(object : AdapterView.OnItemSelectedListener {
            override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
                Log.i(
                    TAG,
                    "SELECTED TONE = $position "
                )
            } // to close the onItemSelected

            override fun onNothingSelected(parent: AdapterView<*>?) {}
        })

        rootView.confirmButton.setOnClickListener {

            if (alarmLabelEditTextTextMultiLine.text.toString() == ""){
                alarm.alarmLabel = "Label"

            }
            else {
                alarm.alarmLabel = alarmLabelEditTextTextMultiLine.text.toString()
            }

            alarmRepository.updateAlarm(alarm)
            (activity as MainActivity).alarmListRefresh()
            (activity as MainActivity).rvSet()
            dismiss()


        }

        //rootView.alarmLabelEditTextTextMultiLine.setSelection()


        return rootView
    }





}

tonePickerDropDown is null, debug and check out why? tonePickerDropDown 是 null,调试看看为什么? You might have to do rootView.tonePickerDropDown instead – Nongthonbam Tonthoi 2 days ago您可能不得不改为使用 rootView.tonePickerDropDown – Nongthonbam Tonthoi 2 天前

2 days ago Nongthonbam Tonthoi's solution worked. 2 天前,Nongthonbam Tonthoi 的解决方案奏效了。 thanks so much.非常感谢。 you are a genius.你是个天才。 Please submit it as an answer so I can accept it.请将其作为答案提交,以便我接受。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我一直得到我的LinearLayout的空引用? - Why do I keep getting a null reference to my LinearLayout? 尝试将包从我的活动传递到我的片段时,为什么我不断收到 null? - Why do I keep getting null when trying to pass a bundle from my activity to my fragment? 为什么我使用带有片段的RecyclerView不断收到空引用错误 - Why do i keep getting a null reference error using RecyclerView with fragments 为什么我不断收到错误无法解析符号片段活动? - why do I keep getting the error Cannot resolve symbol Fragment activity? 在片段微调器中获取空值 - Getting null value in fragment spinner 片段事务出错(空对象参考) - Getting Error on Fragment Transaction(Null Object Reference) 我有一个视图寻呼机,我尝试在主片段中嵌套地图的片段。 为什么我一直得到“错误膨胀类片段”? - I have a view pager and I try to nest a fragment for a map within the main fragment. Why do I keep getting “ Error inflating class fragment”? 为什么我在setOnClickListener上总是收到null异常 - why do I keep getting null exceptions at setOnClickListener 为什么我在我的 RecyclerView 上得到一个空引用 - Why am I getting a null reference on my RecyclerView 为什么我收到空对象引用错误? - Why am I getting a a null object reference error?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM