无法显示之前 activity 的值

unable to display value from previous activity

在第二个 activity 上向 textview 显示值时出现错误

expecting member declaration

这是我的另一个代码 Activity:

   package com.example.trial.sudoku_solver
import android.app.AlertDialog
import android.content.Intent
import android.graphics.Bitmap
import android.media.MediaScannerConnection
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.os.Environment
import android.provider.MediaStore
import android.text.Editable
import android.util.Log
import android.view.View
import android.widget.*
import com.example.trial.sudoku_solver.R
import kotlinx.android.synthetic.main.activity_main.view.*
import java.io.ByteArrayOutputStream
import java.io.File
import java.io.FileOutputStream
import java.io.IOException
import java.util.*
import kotlinx.android.synthetic.main.activity_main.*
import org.w3c.dom.Text

class AnotherActivity : AppCompatActivity() {

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


    var a: String = intent.getStringExtra("text")
    val text1: TextView = findViewById(R.id.textview) as TextView
    text1.text = intent.getStringExtra("text")

}

这是我在 MainActivity

中的函数的部分代码
private fun clickText() {
    val text1: EditText = findViewById(R.id.editText)
    if (text1 != null) {
        //Toast.makeText(this, text1.text, Toast.LENGTH_LONG).show()
        var text1 = text1.editText.toString()
        val intent = Intent(this, AnotherActivity::class.java)
        intent.putExtra("text",text1)
        startActivity(intent);
    }
}

Activity 中没有 settext 方法。您应该像这样将文本字符串设置为 TextView

var a: String = intent.getStringExtra("text")
val text1 = findViewById(R.id.textview) as TextView
text1.text = a

另外,当你用kotlin写代码的时候,在activity中是没有必要使用findViewById的。由于合成属性,可以通过 xml 文件中的 ID 访问视图。因此,您的三行代码可以替换如下:

textview.text = intent.getStringExtra("text")

同时将MainActivity中的代码块改成:

private fun clickText() {
    val editText1: EditText = findViewById(R.id.editText)
    if (editText1 != null) {
        //Toast.makeText(this, text1.text, Toast.LENGTH_LONG).show()
        var text1 = editText1.text.toString()
        val intent = Intent(this, AnotherActivity::class.java)
        intent.putExtra("text", text1)
        startActivity(intent);
    }
}

The easiest way to do this :

Intent intent = new Intent(getBaseContext(), Activity2.class);
intent.putExtra("ID", sId);
startActivity(intent);

Access that intent on next activity

String sId= getIntent().getStringExtra("ID");

希望对您有所帮助

8您需要调用intentfindViewById,然后在调用setContentView之后调用setText

class AnotherActivity : AppCompatActivity() {

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

        var a: String = intent.getStringExtra("text")
        val text1: TextView = findViewById(R.id.textview) as TextView
        text1.text = intent.getStringExtra("text")
    }
}

您需要在 onCreate 方法块中添加这些代码

您应该在方法中添加可执行语句。

因此代码将如下所示:

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

        // add these statement inside onCreate block
        var a: String = intent.getStringExtra("text")
        val text1: TextView = findViewById(R.id.textview) as TextView
        text1.text = intent.getStringExtra("text")
    }