0

To complete Register should be three steps in different classes and method read all string from another activities . When starting project i have some error!

class1 (RegisterActivity.java)

public class RegisterActivity extends AppCompatActivity {

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

            Next = (Button)findViewById(R.id.next);
            Fname = (EditText)findViewById(R.id.fname);
            Lname = (EditText)findViewById(R.id.lname);

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

                    fname = Fname.getText().toString().trim();
                    lname = Lname.getText().toString().trim();

                    Intent intent = new Intent(RegisterActivity.this,RegisterStepTwoActivity.class);
                    startActivity(intent);
                    finish();

                }
            });

        }
    }

class2 (RegisterStepTwoActivity)

public class RegisterStepTwoActivity extends AppCompatActivity {

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

        overridePendingTransition(R.anim.animation,R.anim.animation2);

        Day = (Spinner)findViewById(R.id.spinner_dates);
        Month = (Spinner)findViewById(R.id.spinner_months);
        Year = (Spinner)findViewById(R.id.spinner_years);
        Male = (RadioButton) findViewById(R.id.male);
        Female = (RadioButton) findViewById(R.id.female);
        RG = (RadioGroup)findViewById(R.id.rg);
        Next = (Button)findViewById(R.id.next_s_two);

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

                day = Day.getSelectedItem().toString().trim();
                month = Month.getSelectedItem().toString().trim();
                year = Year.getSelectedItem().toString().trim();

                int id = RG.getCheckedRadioButtonId();
                Male = (RadioButton)findViewById(id);
                Female = (RadioButton)findViewById(id);

                gender = ((RadioButton) findViewById(id)).getText().toString().trim();

                Male.getText();
                Female.getText();

                Intent intent = new Intent(RegisterStepTwoActivity.this,RegisterStepThreeActivity.class);
                startActivity(intent);
                finish();

            }
        });

    }
}

Here , i want to call all there class3 (RegisterStepThreeActivity)

public class RegisterStepThreeActivity extends AppCompatActivity {

    EditText E_mail , Password;
    Button Complete;
    String email , password;

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

        overridePendingTransition(R.anim.animation,R.anim.animation2);
        Complete = (Button) findViewById(R.id.complete);
        E_mail = (EditText) findViewById(R.id.email);
        Password = (EditText) findViewById(R.id.password);

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

                email = E_mail.getText().toString().trim();
                password = Password.getText().toString().trim();

                RegisterActivity reg = new RegisterActivity();
                RegisterStepTwoActivity regtwo = new RegisterStepTwoActivity();

                if (!regtwo.day.isEmpty() && !regtwo.month.isEmpty() && !regtwo.year.isEmpty() && !regtwo.gender.isEmpty() &&
                    !reg.fname.equals(reg.Fname) && !reg.lname.isEmpty() && !email.isEmpty() && !password.isEmpty()) {

                    registerUser(reg.fname, reg.lname, regtwo.day, regtwo.month, regtwo.year, regtwo.gender, email, password);

                }

                else {
                    Snackbar.make(v, "Please enter your datalist", Snackbar.LENGTH_LONG).setAction("Action", null).show();
                }
            }
        });
    }

method registerUser can't applied all String! Error :

java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.isEmpty()' on a null object reference

4 Answers4

1

Under your intent declaration try putting this code

intent.putExtra("data", whatever you are trying to parse);

The when you want to retrieve that data on the next activity write

Intent intent = getIntent();
String whatever=intent.getStringExtra("data");

Now you can use the String in whichever way you like.

Tawanda Muzavazi
  • 405
  • 3
  • 14
0
 // Error code 
 RegisterActivity reg = new RegisterActivity();
 RegisterStepTwoActivity regtwo = new RegisterStepTwoActivity();
 if (!regtwo.day.isEmpty() ...) {
    registerUser(reg.fname, reg.lname, ...);
 }

If you want to send data from one activity to another activity then you can put data in a bundle and then pass it using putExtra() method. For more details check answer

Suggestion

Use fragment instead of using three different activity for the registration task. It would be easy for the transition.

Mable John
  • 4,518
  • 3
  • 22
  • 35
  • If you want to read string form resource `getString(@StringRes int resId)` or `context.getString(@StringRes int resId)` – Mable John Aug 24 '17 at 01:40
0

You can use the Bundle in your code.

RegisterActivity

    // Register one
    fname = Fname.getText().toString().trim();
    lname = Lname.getText().toString().trim();

    Intent intent = new Intent(RegisterActivity.this, RegisterStepTwoActivity.class);
    Bundle bundle = new Bundle();
    bundle.putString("fname", fname);
    bundle.putString("lname", lname);
    startActivity(intent, bundle);
    finish();

RegisterStepTwoActivity

    // get intent for Register one
    Intent intent = getIntent();
    Bundle bundle = intent.getExtras();
    if (bundle != null) {
        String fname = bundle.getString("fname");
        String lname = bundle.getString("lname");

        day = Day.getSelectedItem().toString().trim();
        month = Month.getSelectedItem().toString().trim();
        year = Year.getSelectedItem().toString().trim();

        int id = RG.getCheckedRadioButtonId();
        Male = (RadioButton) findViewById(id);
        Female = (RadioButton) findViewById(id);

        gender = ((RadioButton) findViewById(id)).getText().toString().trim();

        bundle.putString("fname", fname);
        bundle.putString("lname", lname);
        bundle.putString("day", day);
        bundle.putString("month", month);
        bundle.putString("year", year);
        bundle.putString("gender", gender);
        Intent intent2 = new Intent(RegisterStepTwoActivity.this, RegisterStepThreeActivity.class);
        startActivity(intent2, bundle);
        finish();
    }

RegisterStepThreeActivity

    email = E_mail.getText().toString().trim();
    password = Password.getText().toString().trim();

    Intent intent = getIntent();
    Bundle bundle = intent.getExtras();
    if (bundle != null) {

        String fname = bundle.getString("fname");
        String lname = bundle.getString("lname");
        String day = bundle.getString("day");
        String month = bundle.getString("month");
        String year = bundle.getString("year");
        String gender = bundle.getString("gender");

        if (!fname.isEmpty() && !lname.isEmpty() && !day.isEmpty() && !month.isEmpty() &&
              !year.isEmpty() && !gender.isEmpty()&&!email.isEmpty() && !password.isEmpty()) {

            registerUser(fname, lname, day, month, year, gender, email, password);

        }
    }
KeLiuyue
  • 8,149
  • 4
  • 25
  • 42
0

save value/values in shared preferences as : `

editor.putString(KEY_USERNAME,fNAme).commit;

and retrieve it any anywhere you required as :

String username= mPreferenences.getString(KEY_USERNAME,"defValue");

repeat the same procedure for other value as well.

Hope,you got your answer

Inderjeet Singh
  • 111
  • 1
  • 6