1

The code below is the source code for the third and fourth activity.The third activity receives a user input and performs a mathematical operation on it. Then both the original and processed values are send to the fourth activity where it needs to be displayed. I am not quite familiar with the use of intents. Can somebody check my code? It's not working.

Third Activity

public class Third extends Activity {

double x=0, val1=0;


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

    final EditText et;
    final Button b;

    et = (EditText) findViewById(R.id.editText1);
    b = (Button) findViewById(R.id.button1);

    b.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            Intent intent = new Intent(Third.this, Fourth.class);
            intent.putExtra("thetext", et.getText().toString());
            startActivity(intent);

            x=Double.parseDouble(et.getText().toString());

            val1=(x*.04);

            Intent in1 = new Intent(Third.this, Fourth.class);
            in1.putExtra("thevalue1",val1);
            startActivity(in1);

        }


  });

}
}

Fourth Activity:

public class Fourth extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.fourth);

    TextView tv = (TextView) findViewById(R.id.textView1);
    tv.setText(getIntent().getExtras().getString("thetext"));


    TextView wt1=(TextView) findViewById(R.id.textView12);
    wt1.setText(getIntent().getExtras().getDouble("thevalue1"));
} 

}
Nabin
  • 11,216
  • 8
  • 63
  • 98
user3211607
  • 35
  • 2
  • 11

3 Answers3

2

You need to put both extras into one Intent and only call startActivity(...) once. Change the code of your Third Activity (in the onClick(...) method) as follows...

Intent intent = new Intent(Third.this, Fourth.class);
intent.putExtra("thetext", et.getText().toString());
x=Double.parseDouble(et.getText().toString());
val1=(x*.04);
intent.putExtra("thevalue1",val1);
startActivity(intent);
Squonk
  • 48,735
  • 19
  • 103
  • 135
  • Got it! How about the Fourth activity? How to extract the two values from the bundle? – user3211607 Aug 23 '14 at 15:13
  • @user3211607 : Just it as you show in the code you posted. It should work. – Squonk Aug 23 '14 at 15:26
  • It is showing error: The method setText(CharSequence) in the type TextView is not applicable for the arguments (double) – user3211607 Aug 23 '14 at 15:52
  • That's because you're trying to set the text of a `TextView` with a `Double`. Just do the reverse of what you did in Third `Activity` and convert back to a `String` using `String.valueOf(...)`. – Squonk Aug 23 '14 at 16:25
  • Sir, one more doubt.. Can I send multiple number of values added to the same bundle in the same way as shown above? Is there any limit? – user3211607 Aug 23 '14 at 17:27
  • Yes you can send multiple values and there isn't a limit as long as they all have a unique 'key'. – Squonk Aug 24 '14 at 04:54
  • Thanks this worked for me. @Squonk please do you think you could give me your opinion on this question http://stackoverflow.com/questions/25598696/recommended-way-order-to-read-data-from-a-webservice-parse-that-data-and-inse – Axel Sep 02 '14 at 03:16
1

You are starting 2 different activities. I think what you want is to set multiple intent extras instead and then start the activity:

@Override
public void onClick(View v) {
    Intent intent = new Intent(Third.this, Fourth.class);

    intent.putExtra("thetext", et.getText().toString());

    x=Double.parseDouble(et.getText().toString());
    val1=(x*.04);
    intent.putExtra("thevalue1",val1);

    startActivity(intent);
}

In your example you start 1 activity with only the text and another which has only the double. Yet in both activities you are trying to get both of the values.

Also note that for final variables you need to set them when declaring like this:

final EditText et = (EditText) findViewById(R.id.editText1);
final Button b = (Button) findViewById(R.id.button1);
Simas
  • 43,548
  • 10
  • 88
  • 116
0

In fourth activity do this

TextView tv = (TextView) findViewById(R.id.textView1);
    tv.setText(getIntent().getStringExtra("thetext"));


    TextView wt1=(TextView) findViewById(R.id.textView12);
    wt1.setText(getIntent().getDoubleExtra("thevalue1"));

The getExtras() method is for bundle, you storing in third activity in intent

in1.putExtra("thevalue1",val1);
Akhil Jain
  • 13,872
  • 15
  • 57
  • 93
  • This won't solve anything. The extras of an `Intent` are carried in a `Bundle`. Calling `getStringExtra(...)` and `getDoubleExtra(...)` are simply convenience methods for calling `getExtras().getString(...)` and `getExtras().getDouble(...)`. – Squonk Aug 23 '14 at 14:57
  • When i try that iam getting an error: The method getDoubleExtra(String, double) in the type Intent is not applicable for the arguments (String) – user3211607 Aug 23 '14 at 15:09
  • try this `wt1.setText(String.valueOf(getIntent().getDoubleExtra("thevalue1")));` – Akhil Jain Aug 24 '14 at 07:44