Home > Back-end >  RecyclerView setOnClickListener applicationContext problem
RecyclerView setOnClickListener applicationContext problem

Time:12-18

Hi I have a following problem. My project has two files MainActivity.kt and RecyclerAdapter.kt I try to use text to speech engine in the latter one but TextToSpeech asks for applicationContext. How can I provide one?

I tried to set up TTS on MainActivity.kt making a dedicated method for which I called from within RecyclerAdapter.kt but this crashed my app giving me this fatal exception

java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context android.content.Context.getApplicationContext()' on a null object reference

Help please!

package rlk.com.abc

import android.speech.tts.TextToSpeech
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import android.widget.Toast
import androidx.recyclerview.widget.RecyclerView
import java.util.*

class RecyclerAdapter(displayList : MutableList<String>) : RecyclerView.Adapter<RecyclerAdapter.ViewHolder>()  {

    private var displayList: MutableList<String> = displayList

    
    lateinit var tts : TextToSpeech
    var es = Locale("es", "es-ES")



    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerAdapter.ViewHolder {

        val v = LayoutInflater.from(parent.context).inflate(R.layout.card_layout,parent,false)
        return ViewHolder(v)

    }

  
    override fun onBindViewHolder(holder: RecyclerAdapter.ViewHolder, position: Int) {

        holder.tvFrom.text = displayList[position]
           


    }

    
    override fun getItemCount(): Int {
        return displayList.size 
    }

    
    inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView){
        var tvFrom : TextView

        init {
            tvFrom = itemView.findViewById(R.id.tvFrom)

            
            itemView.setOnClickListener{

                val position : Int = adapterPosition
                Toast.makeText(itemView.context,"you clicked on ${displayList[position]}", Toast.LENGTH_SHORT).show()

//The problem is in the line below
                tts = TextToSpeech(applicationContext, TextToSpeech.OnInitListener {
                    if (it==TextToSpeech.SUCCESS) {

                        tts.language = es
                        tts.setSpeechRate(1.0f)
                        tts.speak("this is an example text ", TextToSpeech.QUEUE_ADD,null)


                    }
                })

            }
        }
    }


}

Thank you

CodePudding user response:

You can use the Context from itemView in here:

TextToSpeech(itemView.context, TextToSpeech.OnInitListener { ... })

Unless you tried this (which may be what you were saying in your second paragraph), you could also send in a function to the RecyclerAdapter that is called when you click the itemView, and then you do all the TTS stuff in your Activity.

Also, one last tip: check out ListAdapter for your parent class, it'll save you a bit of trouble.

  • Related