Jagatjeet
Jagatjeet

Reputation: 91

getExtra() is giving a null value when referenced inside onClickListener

I am not sure why I am getting a null for activation code below. Any pointers/explanation on how it works would be really helpful. Thanks

protected void onCreate(Bundle savedInstanceState) {
.
.
.

    Bundle bundle = getIntent().getExtras();
    final String activationcode = bundle.getString("activationcode");
    etActivationCode = (EditText)findViewById(R.id.et_LoginForgotPasswordOk_Code);

    btnForgotPasswordOk = (Button)findViewById(R.id.btn_LoginForgotPasswordOk_OK);
    btnForgotPasswordOk.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            String inputActivationCode = etActivationCode.getText().toString();
            if( inputActivationCode == activationcode){ 
          //the value of activationcode above is null. Hence doesn't match ever
            }
        }
    });

However if I do something like the following, the value of activation code can be referenced

@Override
        public void onClick(View arg0) {
            new CheckMatch().execute(activationcode);
           //the value of activationcode can now be accessed in the AsyncTask class CheckMatch
        }

Upvotes: 0

Views: 88

Answers (2)

StenSoft
StenSoft

Reputation: 9609

== compares instances (pointers if you are familiar with C), not values. You should use inputActivationCode.equals(activationcode).

Upvotes: 1

Saurabh Rajpal
Saurabh Rajpal

Reputation: 1557

You should ideally be using equalsIgnoreCase() for comparing two strings. If it still doesn't work, try converting your activationCode variable into a global variable (by declaring it outside onCreate, within the class). You can assign the value to this variable inside onCreate though (like how you are doing right now).

Upvotes: 1

Related Questions