I have a ListView with clickable ListItems that display a Friend objects name and birthday. The onClick is supposed to start a new Activity with an edit page where you can change the name and birthday of said friend. For this I start a new activity with an Intent containing a Serialized Friend object.
The Activity works perfectly as it should as long as I don't add the Friend object to the intent:
intent.putExtra("friend", friendList.get(position) as Serializable)
but as soon as I add this line of code before starting the activity the screen just turns black. The program doesn't crash and there are no error messages, just a black screen.
MainActivity
class MainActivity : Activity() {
private var friendList : ArrayList<Friend> = arrayListOf()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
initButtons()
}
private fun initButtons() {
val registerButton = findViewById<Button>(R.id.register_friend)
registerButton.setOnClickListener {
val name = findViewById<EditText>(R.id.name).text.toString()
val birthday = findViewById<EditText>(R.id.birthday).text.toString()
if(name != "" && birthday != "") {
friendList.add(Friend(name,birthday))
}
}
val showFriendsButton = findViewById<Button>(R.id.show_friends)
showFriendsButton.setOnClickListener {
val listView = findViewById<ListView>(R.id.friend_list_view)
val adapter = FriendListAdapter(this, friendList)
listView.adapter = adapter
listView.choiceMode = ListView.CHOICE_MODE_SINGLE
if(listView.getVisibility() == View.VISIBLE){
listView.setVisibility(View.INVISIBLE)
}else {
listView.setVisibility(View.VISIBLE)
}
listView.setOnItemClickListener { parent, view, position, id ->
val intent = Intent("android.intent.action.EDIT")
intent.putExtra("friend", friendList.get(position) as Serializable)
startActivity(intent)
}
}
}
}
EditActivity
class EditFriend : Activity(){
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.edit_friend)
val friend: Friend = intent.getSerializableExtra("friend") as Friend
findViewById<EditText>(R.id.name).setText(friend.name)
findViewById<EditText>(R.id.birthday).setText(friend.birthday)
val saveButton = findViewById<Button>(R.id.save)
saveButton.setOnClickListener {
val name = findViewById<EditText>(R.id.name).text.toString()
val birthday = findViewById<EditText>(R.id.birthday).text.toString()
if(name != "" && birthday != "") {
friend.name = name
friend.birthday = birthday
finish()
}
}
}
}
Friend
data class Friend(var name : String?, var birthday : String?) : Serializable