0

I created a Method for my MainActivity to pass a String to my SecondActivity.

public void convertCurrency(View view)
{
   intent.putExtra("NO", "My String");
   startActivity(intent);
}

But in my SecondActivity in my OnCreate Method

    Button back = (Button) findViewById(R.id.back);
    TextView t = (TextView) findViewById(R.id.first_text);

    Intent intent = new Intent(this, MainActivity.class);

    String g = intent.getStringExtra("NO");
    t.setText(g);

Nothing happens. But Why?

5 Answers 5

1

get Your variable in this way:

String yourVriable = getIntent().getStringExtra("NO")

don't new Your intent

Sign up to request clarification or add additional context in comments.

2 Comments

I tried, but I received an error message "Currency Converter Keeps stopping."
maybe you forgot to add your second activity to the manifest?
0

Your are creating new intent.

In onCreate method call

String data = getIntent().getStringExtra(key);

1 Comment

I tried, but I received an error message "Currency Converter Keeps stopping."
0

replace Intent intent = new Intent(this, MainActivity.class); by Intent intent = getIntent();

When you do intent.getStringExtra("NO"); your intent Object is new, so his Extra is empty.

1 Comment

I tried, but I received an error message "Currency Converter Keeps stopping."
0

You need to change the code in both the activities . You need to create intent and put your string into it, so your code will be.

public void convertCurrency(View view) { 
     Intent intent = new Intent(this, Main2Activity.class);
     intent.putExtra("NO","My String");                  
     startActivity(intent);
 }

Note that in the intent constructor the first argument is the current activity and the second argument will be the activity that you are starting.So in the second activity you need to recieve the string. Code for that will be,

Button back = (Button) findViewById(R.id.back); 
TextView t = (TextView) findViewById(R.id.first_text); 
String g = getIntent().getStringExtra("NO")
t.setText(g);

2 Comments

I tried, but I received an error message "Currency Converter Keeps Stopping".
Can u inclide the stacktrace
0

I resolve the problem myself. There occurs an error if you have two Intent objects with the same name even if they are within separate methods.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.