On Button Click code under Switch Button is not Working

Viewed 32

On Button Click, I want to get data on second activity from edit text only if the Switch button is ON but unable to do this anyone can help me to resolve this issue. Thanks

MainActivity.kt

class MainActivity : AppCompatActivity() {

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

        val nextButton = findViewById<Button>(R.id.nextButton)
        nextButton.setOnClickListener {
            nextPage()
        }
    }
    private fun nextPage(){
        val etName = findViewById<EditText>(R.id.name)
        val etEmail = findViewById<EditText>(R.id.email)
        val etPhone = findViewById<EditText>(R.id.phone)
        val etPassPort = findViewById<CheckBox>(R.id.checkbox)
        var passport = ""
        if (etPassPort.isChecked) { passport = etPassPort.text.toString()  }
        val mName = etName.text.toString()
        val mEmail = etEmail.text.toString()
        val mPhone = etPhone.text.toString()
        val intent = Intent(this@MainActivity, ViewActivity::class.java)
        intent.putExtra("Name", mName)
        intent.putExtra("Email", mEmail)
        intent.putExtra("Phone", mPhone)
        intent.putExtra("CHECKBOX", passport)
        val mySwitch = findViewById<SwitchCompat>(R.id.mySwitch)
        mySwitch.setOnCheckedChangeListener { _, isChecked ->
        if (isChecked) {
                val et = findViewById<EditText>(R.id.sponsorName)
                val mEt = et.text.toString()
                intent.putExtra("SPONSOR", mEt)
        }
    }
        startActivity(intent)

    }
}

SecondActivity.kt

class ViewActivity : AppCompatActivity() {

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

        val intent =  intent
        val mName = intent.getStringExtra("Name")
        val mEmail = intent.getStringExtra("Email")
        val mPhone = intent.getStringExtra("Phone")
        val checkbox = intent.getStringExtra("CHECKBOX")
        val mEt = intent.getStringExtra("SPONSOR")

        //textview
        val resultTv = findViewById<View>(R.id.resultTV) as TextView
        //setText
        resultTv.text = mName+"\n"+mEmail+"\n"+mPhone+"\n"+checkbox+"\n"+mEt

    }

}

Switch button code is working fine when i release it from the Button Click. Under the Button Click only null value is showing in Output.

1 Answers

I know where is the problem, first of all you call the buttonCLick, and inside it you call the method nextPage(), if we look closely, this block of code

val mySwitch = findViewById<SwitchCompat>(R.id.mySwitch)
mySwitch.setOnCheckedChangeListener { _, isChecked ->
if (isChecked) {
        val et = findViewById<EditText>(R.id.sponsorName)
        val mEt = et.text.toString()
        intent.putExtra("SPONSOR", mEt)
}

}

is inside your click button, and by default your switch is not checked, so you pass all data, but intent.putExtra("SPONSOR", mEt) is not executed, so when the second activity receive the data is show a null error because val mEt = intent.getStringExtra("SPONSOR") is null. You have two solution, either you write the block of the switch code outside the click button, or in the second activity you check with a conditionif all your data are not null.

Related