============问题描述============
填写注册信息页面:register.class
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button bn = (Button)findViewById(R.id.bn);
bn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
EditText name = (EditText)findViewById(R.id.name);
EditText pass = (EditText)findViewById(R.id.pass);
RadioButton male = (RadioButton)findViewById(R.id.male);
String gender = male.isChecked()?
"男":"女";
Bundle bundle = new Bundle();
person person = new person(name.getText().toString(), pass.getText().toString(), gender);
bundle.putSerializable("person", gender);
Intent intent = new Intent(MainActivity.this, sec.class);
intent.putExtras(bundle);
startActivity(intent);
}
});
}
获取注册信息页面sec.class
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.result);
TextView name = (TextView) findViewById(R.id.name);
TextView passwd = (TextView) findViewById(R.id.pass);
TextView gender = (TextView) findViewById(R.id.gender);
// 获取启动该Result的Intent
Intent intent = getIntent();
// 直接通过Intent取出它所携带的Bundle数据包中的数据
person p = (person) intent.getSerializableExtra("person");
name.setText("您的用户名为:" + p.getName());
passwd.setText("您的密码为:" + p.getPass());
gender.setText("您的性别为:" + p.getGender());
}
实体类:person.class
public class person implements Serializable{
private int id;
private String name;
private String pass;
private String gender;
public person(String name, String pass, String gender)
{
this.name = name;
this.pass = pass;
this.gender = gender;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPass() {
return pass;
}
public void setPass(String pass) {
this.pass = pass;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
}
异常信息:
java,lang.runtimeexception:unable to start activity componentinfo{com.example,register/com.example,register.sec}:java.lang.classcastexception:java.lang.string cannot be cast to com.example.register.person
============解决方案1============
bundle.putSerializable("person", gender);这句改为bundle.putSerializable("person", person);