I have an activity with a tabbed layout containing multiple different tabs. Each tab contains a number of 'edittext' fields. I have a button on the main activity and on clicking it i want to save the the contents of each edittext field from each tab. Currently i can return an ordinary value from the tab but i can not get the contents of the edittext field.
I have tried creating a 'lateinit var frag1_tenNo : EditText' within the fragment class for a tab. I have initialized it in the onCreateView but the program crashes saying 'lateinit property frag1_tenNo has not been initialized'
fragment code
class frag1: Fragment() {
lateinit var frag1_tenNo : EditText
override public fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val viewFrag1 = inflater.inflate(R.layout.frag1, container, false)
frag1_tenNo = viewFrag1.findViewById(R.id.survey_tenantNo)
return viewFrag1
}
fun saveFrag1Data(): String {
var data1 = frag1_tenNo.text.toString()
return data1
}
activity code - to retrieve data
save_btn.setOnClickListener {
Log.d("Survey","Change Button Clicked")
val test = frag1().saveFrag1Data()
Log.d("Survey","Returned value : $test")
}
val test = frag1().saveFrag1Data()
Here you create a new fragment. OnCreate
method of this fragment isn't called yet. So quite predictably you get lateinit property frag1_tenNo has not been initialized
error.