android – I have a spinner I want to take the spinner element save it as a string and pass it to another activity in kotlin-ThrowExceptions

Exception or error:

I have a spinner

 val spinner = findViewById<Spinner>(R.id.categories_spinner)
                val vehicle = arrayOf(
                    "Автокраны",
                    ...
                )
                val adapter = ArrayAdapter<String>(this, R.layout.custom_spinner, vehicle)

                adapter.setDropDownViewResource(R.layout.custom_spinner)
                spinner.adapter = adapter

                spinner.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
                  override fun onItemSelected(parent: AdapterView<*>, view: View, position: Int, id:Long) {

                  if (position == 0) {
                      visibility()
                      ttx_constraint_0.visibility = View.VISIBLE
                      image_check.setOnClickListener {
                          mkVehicleAutocranes()
                          mViewModel.mkVehicleCompletedEvent.observe(this@AddVehicleActivity, Observer {
                                 startActivity(Intent(this@AddVehicleActivity, ListAutocranes::class.java))
                                })
                            }
                        }
                        ...

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

On button click I call the function

 private fun mkVehicleAutocranes() {
            mViewModel.mkAutocranes(
                categories_spinner.getItemAtPosition(0).toString()
            )
        }

I think i save value element of spinner and pass it to another activity. But it’s not work.
Please! Somebody help me!

How to solve:

Leave a Reply

Your email address will not be published. Required fields are marked *