Android Studio Kotlin app "keeps stopping" when I run it

Chadcock

I am just trying to set up a spinner configured as a dropdown menu, but I can't even test my app to see if it works because every time I run it, it immediately crashes. I know that my issue is related to a null object reference at line 21 in my MainActivity.kt file. Here is the problem code: val spinner: Spinner = findViewById<Spinner>(R.id.locations) The id of the spinner is locations, so I'm not sure why this is coming back as a null value. Here is also the full code for the file:


import android.app.Activity
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.Spinner
import android.widget.ArrayAdapter
import android.widget.AdapterView

private var userLocation: Any = ""
private var userDestination: Any = ""


class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
    }

    val spinner: Spinner = findViewById<Spinner>(R.id.locations)
    val locationsAdapter: ArrayAdapter<CharSequence> = ArrayAdapter.createFromResource(
        this,
        R.array.rooms,
        android.R.layout.simple_spinner_item
    ).also { adapter ->
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
        spinner.adapter = adapter
    }
}

class SpinnerActivity : Activity(), AdapterView.OnItemSelectedListener {
    override fun onItemSelected(parent: AdapterView<*>, view: View?, pos: Int, id:Long) {
        userLocation = parent.getItemAtPosition(pos)
        val spinner: Spinner = findViewById(R.id.locations)
        spinner.onItemSelectedListener = this
    }
    override fun onNothingSelected(parent: AdapterView<*>) = Unit
}
MACROSystems

Declare spinner global for MainActivity:

private lateinit var spinner: Spinner

On the method onCreate initialise the spinner and set its adapter, I would recommend you to set the array adapter as follows:

spinner = findViewById<Spinner>(R.id.locations)

spinner.adapter = ArrayAdapter(this, R.layout.drop_down_generic_item, resources.getStringArray(R.array.rooms))

I don't really understand the SpinnerActivity you showed in the question, sorry. But in MainActivity you can set the spinner.onItemSelectedLister() as follows:

spinner.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
            override fun onItemSelected(adapterView: AdapterView<*>?, view: View?, position: Int, p3: Long) {
                val selectedRoom = adapterView?.getItemAtPosition(position)
            }

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

I don't want to confuse you, but have a look at a TextInputLayout with an AutoCompleteTextView, just as a hint. :D

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

I get an error when running an android studio app which used to run perfectly

my app crashes when opening on an android device 'app keeps stopping' error

App keeps stopping whenever I open RecyclerView activity

I created a clicker app in android studio. However, when run, the app crashes after "incrementCount()" is called. Why?

error when i try to run app on emulator android studio

App keeps stopping and I don't know what's going on

Android Studio: lots of errors when I run my app

When I run my app on Android Studio, emulator starts but it says that the test app crashed

When I run app in android studio app store in internal memory.after moving also it doesn't move to sd card

Android: Package installer keeps stopping

missing return statement when I run my app in Android Studio

Android application keeps on stopping

Can I run Android Studio 3.0 without Kotlin?

Android Studio : When ever i run my app, it says Unfortunately app has stopped

App Keeps Stopping when pressing a button

"App name" keeps stopping when I click the "TRANSLATE" button.

Unable to instantiate activity ComponentInfo (App keeps stopping)

AndroidStudio - CompileError: (App keeps stopping) -> RuntimeException: Unable to instantiate activity

How to run kotlin app from android studio?

How can I run the console code of Kotlin in Android Studio?

No error but the app keeps stopping in android studio Java

My app works fine when I run it from android studio, but doesn't work from google play

App crashes at launch "Appname keeps stopping". ClassNotFoundException and InflateException

App keeps stopping, logcat error not understandable

unable to run app on android emulator, App keeps stopping

Xamarin.Android Keeps Stopping

i have a problem in my flutter App when project run in android studio

app keeps stopping error on Android Studio

How to prevent Android Studio to automatically switch to "Run" tab when I start the app