У меня есть два занятия
- 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"
}
}
}
}
Действие в Spinner
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"
}
}
}
}
}