1

I have fragment in MainActivity which display a list of categories, i want to add an option when the user click on the categorie item, A new activity should start (MainActivity2) which contains a fragment which display a list of Articles.

the problem is when i put the method Intent that open an activity i have multiples errors.

Here is my MainActivity2:

package com.mbds.news

import android.content.Intent
import android.os.Bundle
import com.google.android.material.floatingactionbutton.FloatingActionButton
import com.google.android.material.snackbar.Snackbar
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.ContextCompat
import androidx.fragment.app.Fragment
import com.mbds.news.fragments.ArticlesFragment
import com.mbds.news.fragments.CategoriesFragement

class MainActivity2 : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main2)

        changeFragment(ArticlesFragment())
    }
}

/**
 * Ajouter le fragmet [ComputationFragment] dans l'activité
 */
fun MainActivity2.changeFragment(fragment: Fragment) {
    supportFragmentManager.beginTransaction().apply {
        //3) on remplace le contenu du container
        replace(R.id.fragment_container, fragment)
        //4) on ajoute la transaction dans la backstack
        addToBackStack(null)
    }.commit()
    // 5) on commit la transaction
}

Here is where i put my redirection :

class  CategoriAdapter(private val dataset: List<Category>) :
    RecyclerView.Adapter<CategoriAdapter.ViewHolder>() {

    class ViewHolder(val root: View) : RecyclerView.ViewHolder(root) {


        fun bind(item:Category ) {
            var txtname = root.findViewById<TextView>(R.id.category_name)
            val imageView = root.findViewById<ImageView>(R.id.category_image)

            imageView.setOnClickListener {

                val intent = Intent(this, MainActivity2::class.java)
                startActivity(intent)
            }

            txtname.text = item.name

            Glide
                .with(root)
                .load(item.image)
                .centerInside()
                .placeholder(R.drawable.placeholder)
                .into(imageView);
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val rootView = LayoutInflater.from(parent.context)
            .inflate(R.layout.list_item, parent, false)
        return ViewHolder(rootView)
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        holder.bind(dataset[position])
    }
    override fun getItemCount(): Int = dataset.size
}

Here is my logs :

   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

No value passed for parameter 'p1'
No value passed for parameter 'p2'
Tugay
  • 2,057
  • 5
  • 17
  • 32
zak92
  • 77
  • 7

1 Answers1

1

You haven't posted the error but I assume it's because

            val intent = Intent(this, MainActivity2::class.java)
            startActivity(intent)

"this" isn't referencing an Activity or Context as it should, Kotlin views have a context property, so try

            val intent = Intent(imageView.context, MainActivity2::class.java)

EDIT: second line:

            imageView.context.startActivity(intent)
Merthan Erdem
  • 5,598
  • 2
  • 22
  • 29