启动新的意图时,意图状态的附加物。

问题描述 投票:0回答:1

我有一个主活动A和一个不同的活动B,我在B上执行了一个操作,并在extras中返回一个ArrayList给主活动A.然后我在A上启动了一个Filepicker意图。

anotherRow就是ArrayList。

活动B

 val intent = Intent(this, MainActivity::class.java)
    val bundle = Bundle()
    bundle.putSerializable(Constants.sendAdditionalRowsBack, anotherRow)
    intent.putExtras(bundle)
    startActivity(intent)
    finish()

主要活动

val bundle: Bundle? = intent.extras
    //The below will be done only if bundle is not null
    if (bundle != null) {
        returnedArray = bundle?.getSerializable(Constants.sendAdditionalRowsBack) as ArrayList<bring>
    }else{
        runOnUiThread {
            activity?.makeLongToast("Bundle is null")
        }
    }
android kotlin android-intent nullpointerexception
1个回答
0
投票

这就是最终的工作原理

活动B(发送)

 // intialize Bundle instance
    val b = Bundle()
    b.putSerializable("questions", anotherRow as Serializable?)
    val i = Intent(this, OneMainActivity::class.java)
    i.putExtras(b)
    startActivity(i)
    finish()

活动A(接收)

    //get the bundle
    if (activity?.getIntent()?.extras != null) {
        val b = activity?.getIntent()?.extras
        if (b?.getSerializable("questions") as ArrayList<bring> != null) {
            returnedArray = (b!!.getSerializable("questions") as ArrayList<bring>?)!!
        } else {
            runOnUiThread {
                activity?.makeLongToast("An error occured")
            }
        }
    } else {
        runOnUiThread {
            //activity?.makeLongToast("An internal error occured")
        }
    }
© www.soinside.com 2019 - 2024. All rights reserved.