活动更改状态时如何从变量中恢复内容?

时间:2019-04-24 17:57:35

标签: android kotlin

我有两个活动

  • MainActivity
  • NormalStudentActivity

由于 MainActivity 与其他活动 NormalStudentActivity

共享数据

MainActivity.kt

val intent = Intent(this, NormalStudentActivity::class.java)
intent.putExtra("tipo", "normal")
startActivity(intent)

现在在NormalStudentActivity中,我创建一个常规变量 lateinit var temario:String

class NormalStudentActivity : AppCompatActivity() {

  lateinit var temario:String

  override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_normal_student)

        val data = intent.extras
        tem = data.get("tipo").toString()

        if (tem !=null){
            temario = tem
             // add data to spinner ------------>
             //======================================
            val spnnormal = ArrayAdapter.createFromResource(this,R.array.DistNormal,
                    android.R.layout.simple_spinner_item)
                spnnormal.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
            spnnTipo.adapter = spnnormal
            //=====================================
        }
  }

      override fun onSaveInstanceState(outState: Bundle?) {
        super.onSaveInstanceState(outState)
        outState?.putString("temas",tem)
    }

    override fun onRestoreInstanceState(savedInstanceState: Bundle?) {
        super.onRestoreInstanceState(savedInstanceState)

        val str = savedInstanceState?.getString("temas")
        temario = str.toString()
        Toast.makeText(this, "variable restored --> $temario",Toast.LENGTH_LONG).show()

    }
}

但是它生成一个错误,也就是说,当我旋转设备的屏幕时,我得到了一个错误。

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.mrl.fred.alumn, PID: 31186
    java.lang.IllegalArgumentException: Parameter specified as non-null is null: method kotlin.jvm.internal.Intrinsics.checkParameterIsNotNull, parameter view
        at com.mrl.fred.alumn.NormalStudentActivity$onCreate$1.onItemSelected(NormalStudentActivity.kt)

问题在于变量 temario 会生成空值,并且在 spinner onItemSelected 中,我用它来处理其他事件>



            override fun onItemSelected(parent: AdapterView<*>, view: View, position: Int, id: Long) {
                when(position){
                    1 ->{
                        //hight
                        textView34.text = "Long"
                        txtUnico.text = ""
                        if (temario == "student"){
                            editText2.hint = "Insert Weight"
                        }
                    }
                    2 ->{
                        // number student
                        textView34.text = ""
                        txtUnico.text = ""
                        if (temario == "student"){
                            editText2.hint = "Insert number"
                        }
                    }
                }
            }
  

微调器中的操作


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_normal_student)

        spnnTipo.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
            override fun onNothingSelected(p0: AdapterView<*>?) {
            }
            override fun onItemSelected(parent: AdapterView<*>, view: View, position: Int, id: Long) {
                when(position){
                    1 ->{
                        //data
                        textView34.text = "Density weight"
                        txtUnico.text = ""
                        if (temario == "student"){
                            editText2.hint = "Insert data"
                        }
                    }
                    2 ->{
                        // Student angry
                        textView34.text = ""
                        txtUnico.text = ""
                        if (temario == "student"){
                            editText2.hint = "Insert value"
                        }
                    }
                }
            }
        }

0 个答案:

没有答案