Android/Kotlin: Intent 始终为 NULL

Android/Kotlin: Intent always NULL

我在这里搜索了多个 post,它们似乎都有相同的问题,但原因不同。从第二个 activity 调用它们时,我从我的包中得到 NULL 结果。如果需要,我会 post 更多我的代码;

这是我在初始时打包我的包的地方 activity:

    val extras = Bundle()
    extras.putString("EXTRA_DIFFICULTY", dif_spinner.getSelectedItem().toString())
    extras.putString("EXTRA_CAT", cat_spinner.getSelectedItem().toString())
    intent.putExtras(extras)

当我在我的第二个 activity:

    val bundle :Bundle ?=intent.extras
    val difficultystring = bundle?.getString("EXTRA_DIFFICULTY")
    val catstring = bundle?.getString("EXTRA_CAT")
    txtV.text = "Your difficulty level is " + difficultystring + " and your cat is " + catstring

在这两种情况下,它 return 为空。微调器在创建时初始化,因此应该始终有一个值可以调用,有人能给我指出正确的方向吗?

完整代码(功能较少 - 不会引起任何问题)

Activity 1

class MainActivity : AppCompatActivity() {

    lateinit var btnSTART: Button
    lateinit var switchSNOW: Switch
    lateinit var imageviewSNOW: ImageView
    lateinit var requested_difficulty: String
    lateinit var dif_spinner: Spinner
    lateinit var cat_spinner: Spinner
    var difficulty_array = null

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

        dif_spinner = findViewById(R.id.difficulty_spinner)
        cat_spinner = findViewById(R.id.cat_spinner)
        switchSNOW = findViewById(R.id.snow_switch) as Switch
        imageviewSNOW = findViewById(R.id.imageViewSnow) as ImageView

        imageviewSNOW.setVisibility(View.INVISIBLE);

        switchSNOW.setOnCheckedChangeListener { _, isChecked ->
            if(isChecked){
                imageviewSNOW.setVisibility(View.VISIBLE);
            } else {
                imageviewSNOW.setVisibility(View.INVISIBLE);
            }
        }


        btnSTART = findViewById(R.id.buttonSTART) as Button
        btnSTART.setOnClickListener{
            val intent = Intent(this, Quiz::class.java)
            startActivity(intent)
        }

// Create an ArrayAdapter using the string array and a default spinner layout

// Create an ArrayAdapter using the string array and a default spinner layout
        ArrayAdapter.createFromResource(
            this,
            R.array.difficulty_array,
            android.R.layout.simple_spinner_item
        ).also { adapter ->
            // Specify the layout to use when the list of choices appears
            adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
            // Apply the adapter to the spinner
            dif_spinner.adapter = adapter
        }
        ArrayAdapter.createFromResource(
            this,
            R.array.cat_array,
            android.R.layout.simple_spinner_item
        ).also { adapter ->
            // Specify the layout to use when the list of choices appears
            adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
            // Apply the adapter to the spinner
            cat_spinner.adapter = adapter
        }
        val extras = Bundle()
        extras.putString("EXTRA_DIFFICULTY", dif_spinner.getSelectedItem().toString())
        extras.putString("EXTRA_CAT", cat_spinner.getSelectedItem().toString())
        intent.putExtras(extras)

    }
}

Activity 2

class Quiz : AppCompatActivity() {
        lateinit var txtV: TextView
        lateinit var btn1: Button
        lateinit var btn2: Button
        lateinit var btn3: Button
        lateinit var btn4: Button
        lateinit var viewQ: TextView
        var ans: Int = 0

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




        btn1 = findViewById(R.id.button1) as Button
        btn2 = findViewById(R.id.button2) as Button
        btn3 = findViewById(R.id.button3) as Button
        btn4 = findViewById(R.id.button4) as Button
        viewQ = findViewById(R.id.textview_question)
        txtV = findViewById(R.id.textView_info) as TextView


            ans = GetQuestion(1, rand(1, 6))

        btn1.setOnClickListener{


            val theirans: String = btn1.getText().toString() //this will get a string
            val TheirAnswer = theirans.toInt() //this will get a no from the string

                if(TheirAnswer==ans) {
                    btn1.setBackgroundResource(R.color.lime_green);
                } else {
                    btn1.setBackgroundResource(R.color.red);
                }
                    next()
            };
        btn2.setOnClickListener {

            val theirans: String = btn2.getText().toString() //this will get a string
            val TheirAnswer = theirans.toInt() //this will get a no from the string
            if(TheirAnswer==ans) {
                btn2.setBackgroundResource(R.color.lime_green);
            } else {
                btn2.setBackgroundResource(R.color.red);
            }
            next()
        };
        btn3.setOnClickListener{

            val theirans: String = btn3.getText().toString() //this will get a string
            val TheirAnswer = theirans.toInt() //this will get a no from the string
            if(TheirAnswer==ans) {
                btn3.setBackgroundResource(R.color.lime_green);
            } else {
                btn3.setBackgroundResource(R.color.red);
            }
            next()
        };
        btn4.setOnClickListener{

            val theirans: String = btn4.getText().toString() //this will get a string
            val TheirAnswer = theirans.toInt() //this will get a no from the string
            if(TheirAnswer==ans) {
                btn4.setBackgroundResource(R.color.lime_green);
            } else {
                btn4.setBackgroundResource(R.color.red);
            }
            next()
        };

            val bundle :Bundle ?=intent.extras

            val difficultystring = bundle?.getString("EXTRA_DIFFICULTY")
            val catstring = bundle?.getString("EXTRA_CAT")
            txtV.text = "Your difficulty level is " + difficultystring + " and your category is " + catstring
}

使用下面的答案

   val intent = intent
   val difficultystring = intent.getStringExtra("EXTRA_DIFFICULTY")
    val catstring = intent.getStringExtra("EXTRA_CAT")
    txtV.text = "Your difficulty level is " + difficultystring + " and your cat is " + catstring

@cutiko 提出的问题为我的问题提供了答案。出于某种原因,我将包添加到我的代码的末尾,而不是我的开始 activity 意图。

来自 activity 1 我的代码来自:

btnSTART.setOnClickListener{
            val intent = Intent(this, Quiz::class.java)
            startActivity(intent)
}

&

    val extras = Bundle()
    extras.putString("EXTRA_DIFFICULTY", dif_spinner.getSelectedItem().toString())
    extras.putString("EXTRA_CAT", cat_spinner.getSelectedItem().toString())
    intent.putExtras(extras)

btnSTART.setOnClickListener{
            val intent = Intent(this, Quiz::class.java)
            val extras = Bundle()
            extras.putString("EXTRA_DIFFICULTY", dif_spinner.getSelectedItem().toString())
            extras.putString("EXTRA_CAT", cat_spinner.getSelectedItem().toString())
            intent.putExtras(extras)
            startActivity(intent)
}