-6

I get 3 data string from editText (in 1 activity), and then go to next activity

button.setOnClickListener(new View.OnClickListener() {

    public void onClick(View view) {

        a=editText.getText().toString();

        b=editText2.getText().toString();

        goToActivity2();
    }
});

and in 2 activity, i want to set new variables user1, user2, like this

public String user1 = a(from 1 activity);
public String user2 = b(from 1 activity);

Is it possible ? I cant found information about variables in my basics books java.

lukash
  • 3
  • 4

1 Answers1

1

Pass value of string to Activity2:

button.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
    String a = editText.getText().toString();
    String b = editText2.getText().toString();

   Intent intent = new Intent(your_activity.this, Activity2.class);
   intent.putExtra("a_value", a);
   intent.putExtra("b_value", b);
   startActivity(intent);
 }
});

Retrieve the value in 2nd Activity:

public String user1;
public String user2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
user1 = getIntent().getExtras().getString("a_value");
user2 = getIntent().getExtras().getString("b_value");
          ...........
                    ...........
W4R10CK
  • 5,502
  • 2
  • 19
  • 30
  • intent.putExtra("a", a); --> a is variable, so what is "a" ? – lukash Jan 12 '17 at 09:38
  • error with this: java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{ru.russian.app/ru.russian.app.MapsActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Intent.getStringExtra(java.lang.String)' on a null object reference – lukash Jan 12 '17 at 09:49
  • when i write something in edittext and click the button, app still 'crash' with this same error ;/ – lukash Jan 12 '17 at 10:11
  • now this error: java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{ru.russian.app/ru.russian.app.MapsActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.os.Bundle android.content.Intent.getExtras()' on a null object reference – lukash Jan 12 '17 at 10:23
  • 1st block with code i put in onCreate() in 1Activity, 2 block of code with user1 and user 2 in 2Activity but over onCreate with other variables – lukash Jan 12 '17 at 10:45
  • but when i put this in onCreate, i cant use user1 and user2 in other method – lukash Jan 12 '17 at 10:47
  • 'public' is not allowed in onCreate() – lukash Jan 12 '17 at 10:58
  • when i try to put in onCreate() this: "public String user1 = getIntent().getExtras().getString("a_value");" Androidstudio underlines 'public' and say "modifier 'public is not allowed here" – lukash Jan 12 '17 at 11:04
  • i still see old code "edited 1 hour ago" – lukash Jan 12 '17 at 11:09
  • Its edited, ago – W4R10CK Jan 12 '17 at 11:14
  • you are awesome, all works, good luck in life! – lukash Jan 12 '17 at 11:23