intent.putExtra: None 以下函数可以使用提供的参数调用

intent.putExtra: None of the following functions can be called with the arguments supplied

我正在尝试通过 putExtra 的意图将数据从一个 activity 传递到另一个。问题是,我收到错误消息:可以使用提供的参数调用以下函数的 None。 我不确定为什么它不起作用,因为我引用的变量在取景器中。关于发生了什么以及如何修复它的任何线索都会有很大帮助。

这是我的回收适配器:

package com.example.newsapp

import android.content.Intent
import android.icu.text.CaseMap
import android.net.Uri
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.AdapterView
import android.widget.ImageView
import android.widget.TextView
import androidx.core.content.ContextCompat.startActivity
import androidx.recyclerview.widget.RecyclerView
import com.example.newsapp.databinding.NewsItemBinding
import com.squareup.picasso.Picasso

class RecyclerAdapter (
   private var Titles: List<String>,
   private var Images: List<String>,
   private var Authors: List<String>,
   private var Descriptions: List<String>
       ) : RecyclerView.Adapter<RecyclerAdapter.ViewHolder>(){

           inner class ViewHolder(
               view: View
           ): RecyclerView.ViewHolder(view){
               private val binding = NewsItemBinding.bind(view)

               val itemTitle: TextView = binding.tvTitle
               val itemImage: ImageView = binding.ivNewsImage

               fun bind(urlToImage:String){
                   Picasso.get().load(urlToImage).into(binding.ivNewsImage)
               }

               init {
                   itemImage.setOnClickListener{
                       val intent = Intent(view.context, PostActivity::class.java)
                       intent.putExtra("title",itemTitle)
                       view.context.startActivity(intent)
                   }
               }
           }

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

   override fun onBindViewHolder(holder: ViewHolder, position: Int) {
       holder.itemTitle.text = Titles[position]
       val itemImage = Images[position]
       holder.bind(itemImage)

   }

   override fun getItemCount(): Int {
       return Titles.size
   }
}

有问题的部分是这个:

init {
   itemImage.setOnClickListener{
   val intent = Intent(view.context, PostActivity::class.java)
   intent.putExtra("title",itemTitle)                                         view.context.startActivity(intent)
    }
}

这是我的主activity:

package com.example.newsapp

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import androidx.recyclerview.widget.LinearLayoutManager
import com.example.newsapp.databinding.ActivityMainBinding
import kotlinx.coroutines.*
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
import java.lang.Exception

class MainActivity : AppCompatActivity() {

   private lateinit var binding: ActivityMainBinding
   private lateinit var adapter: RecyclerAdapter

   private val newsTitles = mutableListOf<String>()
   private val newsImages = mutableListOf<String>()
   private val newsAuthors = mutableListOf<String>()
   private val newsDescriptions = mutableListOf<String>()

   override fun onCreate(savedInstanceState: Bundle?) {
       super.onCreate(savedInstanceState)
       setContentView(R.layout.activity_main)
       binding = ActivityMainBinding.inflate(layoutInflater)
       setContentView(binding.root)
       makeAPIRequest()
   }

   private fun initRecyclerView() {
       adapter = RecyclerAdapter( newsTitles, newsImages)
       binding.rvNews.layoutManager = LinearLayoutManager(this)
       binding.rvNews.adapter = adapter
   }


   private fun addToList(title:String, image:String, author:String, description:String){
       newsTitles.add(title)
       newsImages.add(image)
       newsAuthors.add(author)
       newsDescriptions.add(description)
   }

   private fun makeAPIRequest() {

       val api = Retrofit.Builder()
               .baseUrl("https://newsapi.org/")
               .addConverterFactory(GsonConverterFactory.create())
               .build()
               .create(APIService::class.java)

       GlobalScope.launch(Dispatchers.IO){
           val response = api.getNews()
           val posts = response.body()
           try{
               if (posts != null) {
                   for(art in posts.Articles){
                       Log.i("Main Activity", "Result = $art")
                       addToList(art.Title,art.urlToImage, art.Author, art.Description)
                   }
               }
                   withContext(Dispatchers.Main){
                       initRecyclerView()
                   }
               } catch (e:Exception){
                   Log.e("Main Activity", e.toString())
               }
       }
   }

}
intent.putExtra("title",itemTitle)

这里,itemTitle是一个TextView。您不能在活动之间传递 TextView。您可以切换到:

intent.putExtra("title",itemTitle.text)

...将 TextView 中的文本放入 extra.

此外,您可能需要考虑这些活动是单独的活动还是单个 activity 中的两个片段(或可组合项)。