genespos
genespos

Reputation: 3311

Activity doesn't start when Intent (into ViewHolder) is called

I'm trying to learn kotlin and I'm following a tutorial for a notes app.

At 1:06:19 (testing the app) my code doesn't open the "Update" view when I click the updateButton.

I added a Toastto verify that updateButton.setOnClickListener works and the message is shown but the intent seems not to be started.

I also tryed to change the Activity called and the button works with a different Activity.

So I thought there must be something wrong in the xml file but it is exaclty as shown in the tutorial, except for the name: the xml filename (activity_update_note.xml in my project) was automatically changed when (at 58:00) in the tutorial the name of the kt file was changed (from UpdateActivity.kt to UpdateNoteActivity.kt) and the "refactor" option is called, while, in the tutorial, the name of the xml is still activity_update.xml.

Anyway I don't think the name could be the problem. So: WHERE I NEED TO LOOK TO FIND THE BUG?

edit I found the bug but I still don't know how to solve: In the updateNoteActivity.kt there is the following code:

noteId = intent.getIntExtra("note_id", -1)
if(noteId == -1) {
    Toast.makeText(this, "noteId = -1", Toast.LENGTH_SHORT).show()
    finish()
    return
}

the toast shows noteId = -1

So the transmitted data are missing or wrong... I'm checking

Solved

The error was in putExtra and getIntExtra the 1st was named note.id but the 2nd was named note_id so the value wasn't acquired and the intent was Finished because of missing value

This is intent start from the kt file :

    override fun onBindViewHolder(holder: NoteViewHolder, position: Int) {
        val note = notes[position]
        holder.titleTextView.text = note.title
        holder.contentTextView.text = note.content

        holder.updateButton.setOnClickListener{
            val intent = Intent(holder.itemView.context, UpdateNoteActivity::class.java).apply {
                putExtra("note.id", note.id)
            }
            holder.itemView.context.startActivity(intent)
        }
    }

THANKS TO THE FEW ONES WHO HAVE DEDICATE SOME OF THEIR TIME TO THIS

I leave this question here as an hint for the (not expert) ones checking bugs, hoping it can help.

Upvotes: 2

Views: 82

Answers (0)

Related Questions