Android - 我想从另一个活动或类中调用getText()

时间:2017-08-24 00:55:15

标签: java android

完成注册应该是不同类中的三个步骤,并且方法从另一个活动中读取所有字符串。 启动项目时我有一些错误!

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();

            }
        });

    }
}

在这里,我想调用所有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();
                }
            }
        });
    }

方法registerUser无法应用所有String! 错误:

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

4 个答案:

答案 0 :(得分:1)

根据您的意图声明,请尝试使用此代码

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

您希望在下一个活动中检索该数据的时间

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

现在您可以按照自己喜欢的方式使用String。

答案 1 :(得分:0)

x <- c("key1 :: 0.01",
       "key2 :: 0.02")
strcapture(pattern = "(.*) :: (0\\.[0-9]+)",
           x = x,
           proto = list(key = character(), value = double()))
#>    key value
#> 1 key1  0.01
#> 2 key2  0.02

如果要将数据从一个活动发送到另一个活动,则可以将数据放入一个包中,然后使用putExtra()方法传递它。有关详细信息,请查看answer

<强>建议

使用片段而不是使用三种不同的活动进行注册任务。过渡很容易。

答案 2 :(得分:0)

您可以在代码中使用Bundle

<强> 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);

        }
    }

答案 3 :(得分:0)

将共享首选项中的值/值保存为:`

editor.putString(KEY_USERNAME,fNAme).commit;

并在您需要的任何地方检索它:

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

也为其他值重复相同的过程。

希望,你得到了答案