【问题标题】:Kotlin putExtra none of the following functions can be called with the arguments suppliedKotlin putExtra 不能使用提供的参数调用以下函数
【发布时间】:2018-10-03 14:40:31
【问题描述】:

完成 Kotlin 的新手,试图让三个值从文本框传递到另一个页面,我没有收到任何错误,信息没有被传递或显示我已经尝试了几个小时才能让它工作但无济于事.

请有人指点我一个好的或正确的方向。

我现在已按照页面下方帮助中的说明编辑了我的代码,该页面现在正在接收我无法显示的信息。

PAGE1

package com.example.james.visitorapp

import android.content.Intent
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.provider.AlarmClock.EXTRA_MESSAGE
import android.view.View
import android.widget.EditText

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
    }

    /** Called when the user taps the Send button */
    fun sendMessage(view: View) {
        val editText = findViewById<EditText>(R.id.editText)
        val editText2 = findViewById<EditText>(R.id.editText2)
        val editText3 = findViewById<EditText>(R.id.editText3)
        val message1 = editText.text.toString()
        val message2 = editText2.text.toString()
        val message3 = editText3.text.toString()


        val intent = Intent(this, DisplayMessageActivity::class.java).apply {
            putExtra(EXTRA_MESSAGE, arrayOf(message1, message2, message3))
        }

        startActivity(intent)
    }
}

receiving page

package com.example.james.visitorapp

import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.provider.AlarmClock.EXTRA_MESSAGE
import android.widget.TextView

class DisplayMessageActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_display_message)

        // Get the Intent that started this activity and extract the string
        val message1 = intent.getStringExtra(EXTRA_MESSAGE)
        val message2 = intent.getStringExtra(EXTRA_MESSAGE)
        val message3 = intent.getStringExtra(EXTRA_MESSAGE)
        // Capture the layout's TextView and set the string as its text
        val textView = findViewById<TextView>(R.id.textView).apply {
            text = message1

        }
        val textView2 = findViewById<TextView>(R.id.textView2).apply {
            text = message2

        }
        val textView3 = findViewById<TextView>(R.id.textView3).apply {
            text = message3

        }
    }
}

【问题讨论】:

    标签: android android-studio kotlin


    【解决方案1】:

    请试试这个

    val intent = Intent(this, DisplayMessageActivity::class.java)
    intent.putExtra(EXTRA_MESSAGE_1, message1)
    intent.putExtra(EXTRA_MESSAGE_2, message2)
    intent.putExtra(EXTRA_MESSAGE_3, message3)
    

    【讨论】:

      【解决方案2】:

      putExtra() 没有任何期望 vararg 的重载。

      您可以通过Array 发送消息:

      putExtra(EXTRA_MESSAGES, arrayOf(message1, message2, message3))
      

      并在接收Activity中用getStringArrayExtra()获取,例如:

      // the inferred type of messages will be Array<String>
      val messages = intent.getStringArrayExtra(EXTRA_MESSAGES)
      // joining the messages into a single String and setting it as the text for the TextView
      textView.text = messages.joinToString(separator = " ")
      

      或者用不同的键将它们分开放置:

      putExtra(EXTRA_MESSAGE_1, message1)
      putExtra(EXTRA_MESSAGE_2, message2)
      putExtra(EXTRA_MESSAGE_3, message3)
      

      (然后用getStringExtra()一一获取)

      附带说明,在这里调用apply() 是多余的(在这两种情况下),您可以直接在Intent 实例上调用putExtra()

      val intent = Intent(this, DisplayMessageActivity::class.java)
          .putExtra(EXTRA_MESSAGE_1, message1)
          .putExtra(EXTRA_MESSAGE_2, message2)
          .putExtra(EXTRA_MESSAGE_3, message3)
      

      【讨论】:

      • 是的,我设法让它工作了 :) 使用了 arrayof 并通过 get 字符串获得了它们额外感谢您的帮助,我真的很感激它
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-07-20
      • 2017-10-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多