【问题标题】:Open new Activity when pressing on an item [duplicate]按下项目时打开新活动[重复]
【发布时间】:2020-10-25 22:13:59
【问题描述】:

我在MainActivity 中有显示类别列表的片段,我想在用户单击类别项目时添加一个选项,应该启动一个新活动 (MainActivity2),其中包含一个显示列表的片段文章。

问题是当我使用打开活动的方法Intent 时出现多个错误。

这是我的 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
}

这里是我重定向的地方:

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
}

这是我的日志:

   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'

【问题讨论】:

  • 您还应该考虑添加错误(日志),以便我们可以看到到底出了什么问题。
  • @Cyb3rKo 是的,谢谢我刚刚做了

标签: android kotlin


【解决方案1】:

你没有发布错误,但我认为这是因为

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

“this”没有像它应该引用的那样引用 Activity 或 Context,Kotlin 视图有一个 context 属性,所以试试

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

编辑:第二行:

            imageView.context.startActivity(intent)

【讨论】:

  • 感谢您的评论,当我按照您的建议进行操作时仍然出现错误,我的类型不匹配。必需:找到的上下文:意图
  • 很抱歉忘记了一些东西,请参阅更新
  • @zak92 在哪一行出现错误?
猜你喜欢
  • 2018-02-04
  • 1970-01-01
  • 2018-04-13
  • 1970-01-01
  • 1970-01-01
  • 2012-12-27
  • 2011-11-28
  • 2019-08-24
  • 1970-01-01
相关资源
最近更新 更多