gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 12:20:08 24 4
gpt4 key购买 nike

完成注册需要三个步骤,在不同的类和方法中从另一个 Activity 读取所有字符串。启动项目时出现一些错误!

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不能应用所有字符串!错误:

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

最佳答案

在您的 Intent 声明下尝试放置此代码

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

当您想要在下一个 Activity 写入时检索该数据时

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

现在您可以按照您喜欢的方式使用字符串。

关于java - Android - 我想从另一个 Activity 或类中调用 getText(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45851340/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com