Ashutosh
Ashutosh

Reputation: 125

Passing value from one activity to another using intent

I am making an application in which I have taken 2 screens; Screen A and Screen B. In both the screens, I have taken an edit text and a text view.

In edit text of Screen A, I am taking mobile number which by clicking on text view I am sending it to Screen B and setting it on edit text of Screen B. This process is working successfully.

This is my code on Screen A -

    tvredeem.setOnClickListener(new OnClickListener() {
  @Override
  public void onClick(View v) {
   // TODO Auto-generated method stub
   Intent i = new Intent(getApplicationContext(),
     RedeemPoints.class);
    i.putExtra(mobileno1, mobileno1);
   startActivity(i);
  }
 });

This is my code on Screen B -

      mobileno = (EditText) findViewById(R.id.mobileno);
    Bundle bundle = getIntent().getExtras();
     String message = bundle.getString("mobileno1");
    mobileno.setText(message);

Now, the problem is when I am coming striaght to Screen B, it is giving null pointer exception on this line - mobileno.setText(message); and the application is forcefully shutting down.

What 'if' condition should I use that when I come on Screen B, it doesn't give null pointer exception and edit text should be blank.

The code below should work when I directly come on Screen B. When text is set on editText of Screen B, I want to run this code...

 mobileno.addTextChangedListener(new TextWatcher() {

 @Override
  public void onTextChanged(CharSequence s, int start, int before,
 int count) {
// TODO Auto-generated method stub
String inputNo = s.toString();
if (inputNo.length() == 10) {
 getmobno = inputNo;     
  new Homedata().execute();     
 }   
 }

 @Override
 public void beforeTextChanged(CharSequence s, int start, int count,
  int after) {
 // TODO Auto-generated method stub
 }

 @Override
 public void afterTextChanged(Editable s) {
 // TODO Auto-generated method stub
  }
}  );

Upvotes: 1

Views: 2060

Answers (2)

MysticMagicϡ
MysticMagicϡ

Reputation: 28823

Change

i.putExtra(mobileno1, mobileno1);

to

i.putExtra("mobileno1", mobileno1);

You must pass key value pair. You are passing mobileno1's value in key, too. So you can't retrieve it using key = "mobileno1"

To avoid null pointer use this:

if (getIntent().getStringExtra("mobileno1") != null) {
    edtnum.setText(getIntent().getStringExtra("mobileno1"));
}

Hope its clear :)

Edit:

If you want to execute some code like that, make sure you add the TextChangedListener before you call setText.

Example:

This code didn't work for me:

    setContentView(R.layout.activity_main);
    e = (EditText) findViewById(R.id.etext);
    e.setText(myString);
    e.addTextChangedListener(new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            // TODO Auto-generated method stub
            System.out.println("on");
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
                int after) {
            // TODO Auto-generated method stub

        }

        @Override
        public void afterTextChanged(Editable s) {
            // TODO Auto-generated method stub
            System.out.println("after");
        }
    });

But if I call setText later to adding textChangedListener, it work fine, and I am able to see sysouts in console.

    setContentView(R.layout.activity_main);
    e = (EditText) findViewById(R.id.etext);

    e.addTextChangedListener(new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            // TODO Auto-generated method stub
            System.out.println("on");
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
                int after) {
            // TODO Auto-generated method stub

        }

        @Override
        public void afterTextChanged(Editable s) {
            // TODO Auto-generated method stub
            System.out.println("after");
        }
    });
    e.setText(myString);

Hope it helps.

Upvotes: 2

Venkat
Venkat

Reputation: 64

in screen A, change

i.putExtra(mobileno1, mobileno1);

to

i.putExtra("mobileno1", mobileno1);

and in screen B change

Bundle bundle = getIntent().getExtras();
String message = bundle.getString("mobileno1");

to

String message = getIntent().getStringExtra("mobileno1");

Screen A :-

@Override
public void onClick(View v) {
  // TODO Auto-generated method stub
   Intent i = new Intent(getApplicationContext(),
   RedeemPoints.class);
   i.putExtra("mobileno1", mobileno1);
   startActivity(i);
 }

Screen B: you have set data directly in intent not in bundle so, you should get it from intent dirctly.

the code should look like

 String message ="";
if (getIntent().hasExtra("mobileno1")){
  message =getIntent().getStringExtra("mobileno1");
}

Upvotes: 0

Related Questions