Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Kotlin crashes on Android when selected item view is null

Tags:

android

kotlin

I am getting an error and the app dies when I run it.

This is the error:

07-22 21:02:19.785 3136-3136/? E/dalvikvm: Could not find class 'android.graphics.drawable.RippleDrawable', referenced from method android.support.v7.widget.AppCompatImageHelper.hasOverlappingRendering
07-22 21:02:19.825 3136-3136/? E/dalvikvm: Could not find class 'android.widget.ThemedSpinnerAdapter', referenced from method android.support.v7.widget.AppCompatSpinner$DropDownAdapter.<init>
07-22 21:02:19.915 3136-3136/? E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.vankhanhpr.vidu2, PID: 3136
    java.lang.IllegalArgumentException: Parameter specified as non-null is null: method kotlin.jvm.internal.Intrinsics.checkParameterIsNotNull, parameter selectedItemView
    at com.example.vankhanhpr.vidu2.fragment_main.fragment_mom_baby.Create_File_Mon$onCreate$1.onItemSelected(Create_File_Mon.kt)
    at android.widget.AdapterView.fireOnSelected(AdapterView.java:893)
    at android.widget.AdapterView.selectionChanged(AdapterView.java:880)
    at android.widget.AdapterView.checkSelectionChanged(AdapterView.java:1043)
    at android.widget.AdapterView.handleDataChanged(AdapterView.java:1023)
    at android.widget.AbsSpinner.onMeasure(AbsSpinner.java:180)
    at android.widget.Spinner.onMeasure(Spinner.java:486)
    at android.support.v7.widget.AppCompatSpinner.onMeasure(AppCompatSpinner.java:416)
    at android.view.View.measure(View.java:16628)
    at android.widget.LinearLayout.measureHorizontal(LinearLayout.java:1231)
    at android.widget.LinearLayout.onMeasure(LinearLayout.java:590)
    at android.view.View.measure(View.java:16628)
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
    at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
    at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
    at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
    at android.view.View.measure(View.java:16628)
    at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:719)
    at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:455)
    at android.view.View.measure(View.java:16628)
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
    at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
    at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:139)
    at android.view.View.measure(View.java:16628)
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
    at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
    at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
    at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
    at android.view.View.measure(View.java:16628)
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
    at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
    at android.view.View.measure(View.java:16628)
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
    at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
    at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
    at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
    at android.view.View.measure(View.java:16628)
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
    at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
    at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2298)
    at android.view.View.measure(View.java:16628)
    at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1916)
    at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1113)
    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1295)
    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1000)
    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5622)
    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
    at android.view.Choreographer.doCallbacks(Choreographer.java:574)
    at android.view.Choreographer.doFrame(Choreographer.java:544)
    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
    at android.os.Handler.handleCallback(Handler.java:733)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:212)
    at android.app.ActivityThread.main(ActivityThread.java:5135)[enter image description here][1]

Code:

if(flag!!) {
    var adapter = ArrayAdapter.createFromResource(this, R.array.relationship_arrays, android.R.layout.simple_spinner_item)
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
    var adapter_sex = ArrayAdapter.createFromResource(this, R.array.sex_arrays, android.R.layout.simple_spinner_item)
    adapter_sex.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
    relationship_spinner!!.adapter = adapter
    sex_spinner!!.adapter = adapter_sex
}
else
{
    var adapter = ArrayAdapter.createFromResource(this, R.array.relationship_mom, android.R.layout.simple_spinner_item)
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)

    var adapter_sex = ArrayAdapter.createFromResource(this, R.array.sex_mom, android.R.layout.simple_spinner_item)
    adapter_sex.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)

    relationship_spinner!!.adapter = adapter
    sex_spinner!!.adapter = adapter_sex
}

try {
    relationship_spinner!!.setOnItemSelectedListener(object : OnItemSelectedListener {
        override fun onItemSelected(parentView: AdapterView<*>, selectedItemView: View, position: Int, id: Long) {
            relationship = parentView.getItemAtPosition(position).toString()
        }

        override fun onNothingSelected(parentView: AdapterView<*>) {
            var r = 0
        }
    })

    sex_spinner!!.setOnItemSelectedListener(object : OnItemSelectedListener {
        override fun onItemSelected(parentView: AdapterView<*>, selectedItemView: View, position: Int, id: Long) {
            sex = parentView.getItemAtPosition(position).toString()
        }

        override fun onNothingSelected(parentView: AdapterView<*>) {
            var r = 0
        }
    })
}
catch (e:Exception){}
like image 897
vankhanhpr Avatar asked Jul 22 '17 14:07

vankhanhpr


1 Answers

In onItemSelected(), the selectedItemView argument may be null when nothing is selected. Change its type to View? instead of View and properly handle the case where nothing is selected.

Example (index is also negative when nothing is selected):

override fun onItemSelected(parentView: AdapterView<*>, selectedItemView: View?, position: Int, id: Long) {
    if (position >= 0) {
        sex = parentView.getItemAtPosition(position).toString()
    }
}
like image 74
BladeCoder Avatar answered Oct 21 '22 14:10

BladeCoder