android studio kotlin无法获取数据显示

时间:2018-04-24 10:52:57

标签: android android-studio kotlin kotlin-extension

向Kotlin完成新手,尝试将三个值从文本框传递到另一个页面,我没有收到任何错误信息未被传递或显示我已经尝试了几个小时才能使其与没有用。第1页是我输入数据的地方,第2页是应该显示的地方。

有人能指出我的方向是好还是正确?

我现在已经编辑了我的代码,如下面的帮助中所示,该页面现在正在接收我无法显示的信息。

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

        }
    }
}

1 个答案:

答案 0 :(得分:1)

您应该在MainActivity.kt

中执行以下操作
class MainActivity : AppCompatActivity() {

    var editText : EditText ?= null
    var editText2 : EditText ?= null;
    var editText3 : EditText ?= null;
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        editText = findViewById<EditText>(R.id.editText)
        editText2 = findViewById<EditText>(R.id.editText2)
        editText3 = findViewById<EditText>(R.id.editText3)

    }

    /** Called when the user taps the Send button */
    fun sendMessage(view: View) {

        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_MSG1", message1)
            putExtra("EXTRA_MSG2", message2)
            putExtra("EXTRA_MSG3", message3)
        }

        startActivity(intent)
    }

DisplayMessageActivity.kt

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_MSG1")
        val message2 = intent.getStringExtra("EXTRA_MSG2")
        val message3 = intent.getStringExtra("EXTRA_MSG3")
        // 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

        }
    }
}