如何修复在Kotlin中创建一个Spinner的时候,在一个Fragment的旋转屏幕上指定为非null的参数为null?

你好大家在我的片段,我已经在我的片段中添加了一个微调和一点点的代码来计算痣,但是当我旋转屏幕崩溃,并抛出一个错误,我不知道为什么它崩溃,当试图检查Non-空对象我不是什么错误的原因,这里是错误的:

java.lang.IllegalArgumentException: Parameter specified as non-null is null: method kotlin.jvm.internal.Intrinsics.checkParameterIsNotNull 

这是我的第一个片段文件:

 private var mListener: OnFragmentInteractionListener? = null override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?, savedInstanceState: Bundle?): View? { // Inflate the layout for this fragment return inflater!!.inflate(R.layout.fragment_first, container, false) } override fun onViewCreated(view: View?, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) //activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT) /*Find the id of spinner*/ val spinner = lol /*set an adapter with strings array*/ spinner.adapter = ArrayAdapter(activity, R.layout.support_simple_spinner_dropdown_item, resources.getStringArray(R.array.atoms)) as SpinnerAdapter /*set click listener*/ spinner.onItemSelectedListener = object : AdapterView.OnItemSelectedListener { override fun onItemSelected(parent: AdapterView, view: View, position: Int, id: Long) { val num = when (spinner.selectedItem.toString()) { "H" -> editText.setText("1") "He" -> editText.setText("4") "C" -> editText.setText("12") "O" -> editText.setText("16") else -> editText.setText("") } } override fun onNothingSelected(parent: AdapterView) { /*Do something if nothing selected*/ } } button.setOnClickListener { if ( editText2.text.toString().length > 0 && editText.text.toString().length > 0) { val num2 = editText.text.toString().toDouble() val num1 = editText2.text.toString().toDouble() val num = num1/num2 textView.setText("$num moles") } else { textView.setText("Please Enter a correct value") } } } 

这是我的fragment_first.xml文件:

     

One Solution collect form web for “如何修复在Kotlin中创建一个Spinner的时候,在一个Fragment的旋转屏幕上指定为非null的参数为null?”

添加? 在

view = View?

这里是代码

spinner.onItemSelectedListener = object : AdapterView.OnItemSelectedListener { override fun onItemSelected(parent: AdapterView< *>, view: View?, position: Int, id: Long) { val num = when (spinner.selectedItem.toString()) { "H" -> editText.setText("1") "He" -> editText.setText("4") "C" -> editText.setText("12") "O" -> editText.setText("16") else -> editText.setText("") } }

  • 如何将一个二维数组作为parameter passing给Kotlin中的一个函数
  • Kotlin三元条件运算符
  • Kotlin:对象与同伴对象之间的区别
  • Kotlin的挥发性?
  • 使用kotlin-stdlib 1.1.51,但加载了kotlin-runtime 1.3
  • BiMap /双向HashMap在Kotlin
  • 在Parent类中声明与Child类相同的属性
  • 什么Java 8 Stream.collect等价物在标准Kotlin库中可用?
  • 如何使用Kotlin折叠function将数组转换为地图?
  • 如何用Kotlin React Frontend渲染ChartJs
  • 用Kotlin创建自定义匕首2范围
  • Kotlin language will be the best programming language for Android.