0

Quiero llamar o abrir una actividad desde un fragment en mi aplicacion con kotlin, lo trate de llamar con setOnClickListener pero algo pasa con intent, porque no lo reconoce. Normalmente para llamar a una actividad se usa:

        textView12.setOnClickListener {
            val intent = Intent(this , miactvidad::class.java)
            startActivity(intent)
        }

Pero talvez la forma de llamar a una desde un fragment sea diferente Este es el codigo que tengo:

package com.example.bottom.ui.notifications

import android.content.Intent
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.fragment.app.Fragment
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProviders
import com.example.bottom.R
import com.example.myapplication.olvidaste
import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.android.synthetic.main.activity_main.olvidasteContraseña
import kotlinx.android.synthetic.main.fragment_notifications.*

class NotificationsFragment : Fragment() {

    private lateinit var notificationsViewModel: NotificationsViewModel

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,savedInstanceState: Bundle?): View? {
        notificationsViewModel =
                ViewModelProviders.of(this).get(NotificationsViewModel::class.java)
        val root = inflater.inflate(R.layout.fragment_notifications, container, false)
        textView12.setOnClickListener {
            val intent = Intent(this , olvidaste::class.java)
            startActivity(intent)
        }
        val textView: TextView = root.findViewById(R.id.text_notifications)
        notificationsViewModel.text.observe(viewLifecycleOwner, Observer {
            textView.text = it
        })
        return root
    }
}

Por cierto este es el error que recibo al tratar de ejecutar mi app: e: C:\Users\brand\AndroidStudioProjects\bottom\app\src\main\java\com\example\bottom\ui\notifications\NotificationsFragment.kt: (27, 26): None of the following functions can be called with the arguments supplied: public constructor Intent(p0: Context!, p1: Class<*>!) defined in android.content.Intent public constructor Intent(p0: String!, p1: Uri!) defined in android.content.Intent

Gracias si pueden resolver mi problema

1
  • prueba con this@MiFragmentNameDondeEstaElTextview
    – OsAndNoTi
    el 30 nov. 2020 a las 19:12

1 respuesta 1

0

Lo que pasa es que startActivity es un método de Context, y para crear un Intent explicito también necesitas un Context.

La clase Fragment no hereda de Context, por lo que deberás obtener el Context llamando a requireContext()

textView12.setOnClickListener {
    requireContext().let{
        it.startActivity(Intent(it, olvidaste::class.java))
    }
}

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.