简体   繁体   中英

Problems setting TextView value to a String got from Intent

So... My problem is that when I try to send Intent putExtra , get it in the new class and then put it into a TextView it returns null...

Here is my code:

final String phonenum = text.getText().toString();

 Intent i = new Intent(sms_verification.this, sms_verification_two.class);
                        i.putExtra("num", num);
                        i.putExtra("phonenum" , phonenum);
                        startActivity(i);

And then the "sms_verification_two"

Button next;
EditText code;
TextView phone;

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.sms_verification2);

    next = (Button)findViewById(R.id.button);
    code = (EditText)findViewById(R.id.editText2);
    phone = (TextView)findViewById(R.id.textView);
    String phonenum = getIntent().getStringExtra("phonenum");
    String num = getIntent().getStringExtra("num");


    phone.setText(phonenum);

Make sure the data you put in the intent isn't null. The code itself looks fine to me.

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Button goToNextActivity=(Button) findViewById(R.id.bSendID);
    final EditText etNum=(EditText) findViewById(R.id.etNumID);
    final EditText etPhoneum=(EditText) findViewById(R.id.etPhoneumID);

    goToNextActivity.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            String textFromEtNum=etNum.getText().toString();
            String textFromEtPhoneum=etPhoneum.getText().toString();

            Intent i = new Intent(getApplicationContext(), secondActivity.class);
            i.putExtra("num", textFromEtNum);
            i.putExtra("phonenum" , textFromEtPhoneum);
            startActivity(i);


        }
    });


}


}

second activity:

public class secondActivity extends AppCompatActivity {

Button next;
EditText code;
TextView phone;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_second);

    next = (Button)findViewById(R.id.button);
    code = (EditText)findViewById(R.id.editText2);
    phone = (TextView)findViewById(R.id.textView);
    String phonenum = getIntent().getStringExtra("phonenum");
    String num = getIntent().getStringExtra("num");


    phone.setText(phonenum + " " + num);
}
}

First Activity

  **if your text variable TextView or EditText then write**      

 String phonenum = text.getText().toString();

Intent i = new Intent(sms_verification.this, sms_verification_two.class);
                    i.putExtra("nums", num);
                    i.putExtra("phonenums" , phonenum);
                    startActivity(i);

Second Activity

      public class sms_verification_two extends AppCompatActivity {

      Button next;
      EditText code;
      TextView phone;

     String phonenumss = "",numss = "";

  @Override
   protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_second);

   next = (Button)findViewById(R.id.button);
   code = (EditText)findViewById(R.id.editText2);
   phone = (TextView)findViewById(R.id.textView);

if(getIntent().getStringExtra("phonenums") != null){
   phonenumss = getIntent().getStringExtra("phonenums");
}
if(getIntent().getStringExtra("nums") != null){
   numss = getIntent().getStringExtra("nums");
}


    phone.setText(" " +phonenumss + " " + numss);

} }

Try using this approach on your sms_verification_two activity

String phonenum = "";
String num = "";

Bundle extras = getIntent().getExtras();
if (extras != null) {
    phonenum = extras.getString("phonenum");
    num = extras.getString("num");
}

phone.setText(phonenum + num);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM