我的想法是为什么我的parcelable实现会抛出ClassCastException。我想知道为什么它会抛出异常并找到解决方法。如果我仍然缺乏提供信息以便你们帮助我,请告诉我。
IdModelDTO类
public class IdModelDTO implements Parcelable {
private String type;
private String surname;
private String givenName;
private String middleName;
private String birthDate;
protected IdModelDTO(Parcel in) {
givenName = in.readString();
middleName = in.readString();
surname = in.readString();
birthDate = in.readString();
type = in.readString();
}
public static final Creator<IdModelDTO> CREATOR = new Creator<IdModelDTO(){
@Override
public IdModelDTO createFromParcel(Parcel in) {
return new IdModelDTO(in);
}
@Override
public IdModelDTO[] newArray(int size) {
return new IdModelDTO[size];
}
};
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(givenName);
dest.writeString(middleName);
dest.writeString(surname);
dest.writeString(birthDate);
dest.writeString(type);
}
@Override
public String toString() {
return "IdModelDTO{" + "givenName='" + givenName + '\''
+ ", middleName='" + middleName + '\'' +
"surname='" + surname + '\'' +
"birthdate=''" + birthDate + '\'' +
"type='" + type + '\'' + '}';
}
这是我将它用作捆绑包的地方。在将其发送到下一个活动之前,我甚至可以将其作为捆绑包进行记录:
public void checkMplusRespCodes() {
// Check if response codes are OK/Created:
if(governmentIdRespCode == null){
Log.d(TAG, "Government Id Response code: " + governmentIdRespCode );
}
if(saveSelfieResponse == null){
Log.d(TAG, "saveSelfieResponse: " + saveSelfieResponse);
}
else if(governmentIdRespCode != null && saveSelfieResponse != null){
if(governmentIdRespCode.equals("200") && saveSelfieResponse.equals("200")){
//Create bundle here, then pass it to view:
//Bundle idModelBundle = new Bundle();
//idModelBundle.putParcelable(THISIDMODEL, thisIdModel);
//viewContract.showNextActivity(idModelBundle);
Log.d(TAG, " checkMplusRespCodes: " + idModelDTO.toString());
Bundle idModelBundle = new Bundle();
idModelBundle.putParcelable(THISIDMODEL, idModelDTO);
viewContract.showDoneLoading();
viewContract.showNextActivity(idModelBundle);
}
}
然后在nextsetivity,它抛出ClassCastException:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test1);
/* */
Intent previousIntent = getIntent();
if(previousIntent.getExtras() != null){
Bundle previousExtras = previousIntent.getExtras();
if(previousExtras != null){
IdModelDTO test = previousIntent.getParcelableExtra(THISIDMODEL);
if(test != null){
Log.d(TAG, " asd: " + test.toString());
}
/* Retrieve: */
}
}else{
//proceed.
}
非常感谢任何形式的帮助。
修改 这是方法的日志,而我将Parcelable对象设置为bundle:
checkMplusRespCodes: IdModelDTO{givenName='test', middleName='asd'surname='Test'birthdate=''axx'type='asxd'}
异常发生在它被投射的nextActivity:
IdModelDTO test = previousIntent.getParcelableExtra(THISIDMODEL);
答案 0 :(得分:0)
用此
替换您的代码@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test1);
/* */
Intent previousIntent = getIntent();
if(previousIntent.getExtras() != null){
Bundle previousExtras = previousIntent.getExtras();
if(previousExtras != null){
IdModelDTO test = previousExtras.getParcelable(THISIDMODEL);
if(test != null){
Log.d(TAG, " asd: " + test.toString());
}
/* Retrieve: */
}
}else{
//proceed.
}
答案 1 :(得分:0)
我认为您正在尝试解析不是date
的{{1}}对象。
这就是你得到这个例外的原因。
答案 2 :(得分:0)
好的,我解决了这个问题。我正在将一个Bundle对象传递给我的自定义IdModelDTO,如下所示:
if(previousExtras != null){
IdModelDTO test = previousIntent.getParcelableExtra(THISIDMODEL);
...
我删除了Bundle部分,我把对象放在bundle中,然后使用putExtra让我能够检索对象的类型:
Intent nextActivityIntent = new Intent(this, TestActivityX.class);
nextActivityIntent.putExtra(THISIDMODEL,idModelDTO);
Log.d(TAG, " Show next activity: " + idModelDTO.toString());
startActivity(nextActivityIntent);
然后检索将用于下一个活动:
Intent previousIntent = getIntent();
if (previousIntent.getExtras() != null) {
Bundle previousExtras = previousIntent.getExtras();
if (previousExtras != null) {
// Retrieve:
if (previousExtras.containsKey(THISIDMODEL)) {
// Re-instanciate new user.
IdModelDTO test = previousExtras.getParcelable(THISIDMODEL);
Log.d(TAG, " Previous Details:" + test.toString());
}
}
}
谢谢大家,我非常感谢这些评论。