由于上下文问题,隐式意图不起作用 [英] Implicit Intent is not working due to context issue

查看:135
本文介绍了由于上下文问题,隐式意图不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码:-

收藏活动

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.widget.Toolbar
import androidx.core.content.ContextCompat.startActivity
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import java.security.AccessController.getContext


//this is my calling activity
class FavouriteActivity : AppCompatActivity() {
   override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.favourite_activity)
        val mToolbar: Toolbar = findViewById(R.id.toolbar_favourite)
        setSupportActionBar(mToolbar)
        getSupportActionBar()?.setDisplayHomeAsUpEnabled(true);
        getSupportActionBar()?.setDisplayShowHomeEnabled(true);
        setTitle("Favourite Activity");


        //getting recyclerview from xml
        val recyclerView = findViewById(R.id.recyclerView) as RecyclerView
        //adding a layoutmanager
        recyclerView.layoutManager = LinearLayoutManager(this, RecyclerView.VERTICAL, false)
        //it can be staggered and grid
        //creating our adapter
        val adapter = CustomAdapter(star)   //here I am calling the adapter activity
        //now adding the adapter to recyclerview
        recyclerView.adapter = adapter

    }
  override fun onSupportNavigateUp(): Boolean {
        onBackPressed()
        return true
    }
}

CustomAdapter类

class CustomAdapter(val userList: ArrayList<User>) : RecyclerView.Adapter<CustomAdapter.ViewHolder>() {

    //this method is returning the view for each item in the list
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CustomAdapter.ViewHolder {
        val v = LayoutInflater.from(parent.context).inflate(R.layout.list_layout_favourite, parent, false)
        return ViewHolder(v)
    }


    //this method is binding the data on the list
    override fun onBindViewHolder(holder: CustomAdapter.ViewHolder, position: Int) {
        holder.bindItems(userList[position])

        holder.imgCopy.setOnClickListener(View.OnClickListener {
            holder.shareString(userList[position])
            Toast.makeText(holder.itemView.getContext(),"Copy Button Clicked", Toast.LENGTH_SHORT).show()

        })
    }

    //this method is giving the size of the list
    override fun getItemCount(): Int {
        return userList.size
    }

    //the class is holding the list view
    class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {

        val imgCopy: ImageView = itemView.findViewById(R.id.img_copy) as ImageView

        val textViewName = itemView.findViewById(R.id.tvTitle) as TextView

        fun bindItems(user: User) {

            textViewName.text = user.name

        }

        fun shareString(user: User)
        {
            val message : String = user.name
            val intent = Intent()
            intent.action = Intent.ACTION_SEND
            intent.putExtra(Intent.EXTRA_TEXT,message)
            intent.type = "text/plain"
            startActivity(Intent.createChooser(intent,"Share to :"))    ///Issue occur right here
          }}}

遇到错误:必需的上下文,找到了Intent.

Getting error : Required context , found Intent.

在其他FragmentActivity中工作正常. 我尝试了各种方法来调用上下文.但任何事情都行不通. 我还通过了Fragment活动的上下文,但这也没有用. 请让我知道有什么方法可以启动Intent. 由于我总是遇到错误并因此而陷入困境.

it is working fine in other FragmentActivity. I have tried various methods to called the context. but anything is not working. I have also passed the context from Fragment activity, but that also not worked. Please let me know is there any way to start Intent. As I am always getting error and stuck due to this.

推荐答案

ViewHolder类中可用的startActivity与Activites中可用的startActivity不同.因此,在此方法(在viewholder中可用)中,第一个参数应该是上下文.因此,传递上下文如下:

The startActivity available in the ViewHolder class is different from the one available in activites. So in this method (available in viewholder), the first parameter should be a context. So pass the context as follows:

startActivity(itemView.context, Intent.createChooser(intent,"Share to :"))

这篇关于由于上下文问题,隐式意图不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆