我正在尝试使用POJO类对象将数据从一个活动发送到另一个活动。 但是如果我调用get()方法,该应用程序将无法运行 没有get方法的应用程序运行 包com.example.user.bankemployee;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
public class Employee implements Serializable{
private String employeeName;
private String employeeId;
private String age;
private String gender;
private String phone;
private String email;
private String city;
private List<String>skill=new ArrayList<>();
public Employee() {
}
public Employee(String employeeName, String employeeId, String age) {
this.employeeName = employeeName;
this.employeeId = employeeId;
this.age = age;
}
public String getEmployeeName() {
return employeeName;
}
public String getEmployeeId() {
return employeeId;
}
public String getAge() {
return age;
}
public List<String> getSkill() {
return skill;
}
public void setSkill(List<String> skill) {
this.skill = skill;
}
public String getGender() {
return gender;
}
public void Next(View view) {
这是意图代码
Employee employee=new Employee(empName,empId,age);
Intent intent=new Intent(this,Step2.class);
intent.putExtra("step1",employee);
startActivity(intent);
}
这是主要活动
public class EmployeeList extends AppCompatActivity {
String empName,empId,age,Gender,phone,email,city;
List<String> skill=new ArrayList<>();
TextView tb;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_employee_list);
tb=findViewById(R.id.TB);
Employee employee=(Employee) getIntent().getSerializableExtra("step1");
empName=employee.getEmployeeName();
empId=employee.getEmployeeId();
age=employee.getAge();
如果我这样做,它不会显示任何错误,否则应用会继续停止
public class EmployeeList extends AppCompatActivity {
String empName,empId,age,Gender,phone,email,city;
List<String> skill=new ArrayList<>();
TextView tb;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_employee_list);
tb=findViewById(R.id.TB);
Employee employee=(Employee)
getIntent().getSerializableExtra("step1");
//empName=employee.getEmployeeName();
//empId=employee.getEmployeeId();
//age=employee.getAge();
答案 0 :(得分:0)
与其将“雇员”对象直接放置在意图上,请尝试创建捆绑并将雇员添加到该捆绑中。然后,将捆绑包额外添加到Intent中。 开始活动时,请恢复捆绑包并找到员工。尝试这种方式。
另外,正如Mike M.所说,请确保您在适当的活动中恢复了意图。