title: How change text of textview inside main activity from recyclerview adapter class
body: <p>I'm using a recycler view in fragment which is loaded into main activity.When my recycler view item is clicked i want to set text to textview which is inside main activity.
  I tried doing this:</p>
  
  <pre><code>displayText(MainActivity(),R.id.name,"Text you want to display");
              MainActivity().textView!!.setText(name)
  
   fun displayText(activity: Activity, id:Int, text:String) {
          val tv = activity.findViewById(id) as TextView
          tv.setText(text)
      }
  </code></pre>
  
  <p>But it doesn't work my application crashes
  Please help me debug this as i'm still a newbie any help will be appreciated.</p>
  
  <p>This is my main activity class:</p>
  
  <pre><code>    class MainActivity : AppCompatActivity() {
          private var adapter: Tabadapter? = null
          private var tabLayout: TabLayout? = null
          private var viewPager: ViewPager? = null
          var PlayPauseView : PlayPauseView? = null
          var textView : TextView?=null
           var context : Context?=null
          var slidingUpPanelLayout : SlidingUpPanelLayout?=null
          override fun onCreate(savedInstanceState: Bundle?) {
              super.onCreate(savedInstanceState)
              setContentView(R.layout.activity_main)
              Dexter.withActivity(this)
                      .withPermissions(
                              Manifest.permission.READ_EXTERNAL_STORAGE,
                              Manifest.permission.WRITE_EXTERNAL_STORAGE
                      ).withListener(object : MultiplePermissionsListener {
                          override fun onPermissionsChecked(report: MultiplePermissionsReport) {/* ... */
                          }
  
                          override fun onPermissionRationaleShouldBeShown(permissions: List&lt;PermissionRequest&gt;, token: PermissionToken) {/* ... */
                          }
                      }).check()
              viewPager = findViewById&lt;View&gt;(R.id.viewPager) as ViewPager
              tabLayout = findViewById&lt;View&gt;(R.id.tabLayout) as TabLayout
              adapter = Tabadapter(supportFragmentManager)
              adapter!!.addFragment(Songs(), "Songs")
              adapter!!.addFragment(Playlist(), "Playlist")
              adapter!!.addFragment(Folders(), "Folders")
              adapter!!.addFragment(Albums(), "Albums")
              adapter!!.addFragment(Artists(), "Artists")
              adapter!!.addFragment(Genres(), "Genres")
              viewPager!!.adapter = adapter
              tabLayout!!.setupWithViewPager(viewPager)
              slidingUpPanelLayout = findViewById(R.id.sliding_layout)
              PlayPauseView=findViewById(R.id.play_pause_view)
              PlayPauseView!!.setOnClickListener{v-&gt;
                  PlayPauseView!!.toggle()
              }
              textView = findViewById(R.id.name)
              textView!!.startAnimation(AnimationUtils.loadAnimation(this, R.anim.translate) as Animation)
              slidingUpPanelLayout!!.addPanelSlideListener(object : SlidingUpPanelLayout.PanelSlideListener {
                  override fun onPanelSlide(panel: View, slideOffset: Float) {
  
                  }
  
                  override fun onPanelStateChanged(panel: View, previousState: PanelState, newState: PanelState) {
                      Log.i("State", "onPanelStateChanged $newState")
                  }
              })
  
  
          }
          fun update_counter(value:String) {
              try
              {
                  runOnUiThread() {
                      textView = findViewById(R.id.name)
                      textView !!.setText(value)
                  }
                 // val mHandler = Handler()
  
                  //textView!!.text=value
              }
              catch (ex:Exception) {
                  Log.d("Exception", "Exception of type" + ex.message)
              }
          }
      }
  </code></pre>
  
  <p>This is my adapter class:</p>
  
  <pre><code>class Songsadapter(private val sname: List&lt;String&gt;, private val sartist: List&lt;String&gt;, private val surl : List&lt;String&gt;, internal var mContext: Context) : RecyclerView.Adapter&lt;Songsadapter.ProjectViewHolder&gt;() {
  
      var mPlayer : MediaPlayer?=null
       var ps : Int?=null
      override fun onCreateViewHolder(parent: ViewGroup, i: Int): ProjectViewHolder {
          val layoutInflater = LayoutInflater.from(parent.context)
          val view = layoutInflater.inflate(R.layout.custom_songs, parent, false)
          return ProjectViewHolder(view)
      }
  
      override fun onBindViewHolder(holder: ProjectViewHolder, position: Int) {
          val pos = holder.adapterPosition
          var name = sname.get(pos)
          var artist = sartist.get(pos)
          var songurl = surl.get(pos)
          try {
              var metaRetriver: MediaMetadataRetriever
              metaRetriver = MediaMetadataRetriever()
              metaRetriver.setDataSource(songurl)
              var art: ByteArray
              art = metaRetriver.embeddedPicture
              var songImage = BitmapFactory.decodeByteArray(art, 0, art.size)
              var ob: BitmapDrawable = BitmapDrawable(mContext.getResources(), songImage)
              holder.covimg!!.setBackgroundDrawable(ob)
          }catch (e: Exception){
              e.printStackTrace()
          }
          holder.tvsname.text = name
          holder.tvsartist.text = artist
          holder.itemView.setOnClickListener(View.OnClickListener {
              MainActivity().update_counter(name)
              stoplaying()
              var i:Int = pos
              mPlayer = MediaPlayer()
              mPlayer!!.setAudioStreamType(AudioManager.STREAM_MUSIC)
              mPlayer!!.setDataSource(mContext.getApplicationContext(), Uri.parse(songurl))
              mPlayer!!.prepare()
              mPlayer!!.start()
              mPlayer!!.setOnCompletionListener {
                  i = i+1
                  mPlayer!!.setAudioStreamType(AudioManager.STREAM_MUSIC);
                  mPlayer!!.setDataSource(mContext.getApplicationContext(), Uri.parse(surl.get(i)))
                  mPlayer!!.prepare()
                  mPlayer!!.start()
              }
          })
  
      }
  
      override fun getItemCount(): Int {
  
          return sname.size
  
      }
  
      inner class ProjectViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
  
          var tvsname: TextView
          var tvsartist: TextView
          var covimg: ImageView
  
  
          init {
              tvsname = itemView.findViewById(R.id.sname)
              tvsartist = itemView.findViewById(R.id.sartist)
              covimg = itemView.findViewById(R.id.imgscov)
          }
      }
  fun stoplaying(){
      if (mPlayer != null) {
          mPlayer!!.stop()
          mPlayer!!.release()
          mPlayer = null
      }
  }
  }
  </code></pre>