gpt4 book ai didi

android - Key _id 需要 Parcelable 但值为 java.lang.Long。在 Activity 之间传递 Parceable 对象时

转载 作者:行者123 更新时间:2023-11-30 03:32:50 24 4
gpt4 key购买 nike

我想将自定义类的对象传递给另一个 Activity 。我开始知道 Parceable is wayWAY比序列化更快。但是这样做我得到了异常(exception)

Key _id expected Parceable but value was java.lang.Long. The default value <null> was returned.

我不知道我做错了什么。我遵循了这些教程:

我的对象代码实现了Parcelable如下:

public class CourseNote implements Parcelable {

private long id;
private String noteDescription;
private String noteTitle;
private String creationDate;
private String notePic;
private String noteAudio;
private String noteVideo;
private long courseID_FK;

// ---------------------------------------------------------------------------

public CourseNote() {
super();
}

// ---------------------------------------------------------------------------

public CourseNote(long id, String noteContent, String noteTitle,
String creationDate, String notePic, String noteAudio,
String noteVideo, long courseID_FK) {
super();
this.id = id;
this.noteDescription = noteContent;
this.noteTitle = noteTitle;
this.creationDate = creationDate;
this.notePic = notePic;
this.noteAudio = noteAudio;
this.noteVideo = noteVideo;
this.courseID_FK = courseID_FK;
}

// ---------------------------------------------------------------------------

// parcel constructor
public CourseNote(Parcel in) {
String[] data = new String[8];

in.readStringArray(data);
this.id = Long.parseLong(data[0]);
this.noteDescription = data[1];
this.noteTitle = data[2];
this.creationDate = data[3];
this.notePic = data[4];
this.noteAudio = data[5];
this.noteVideo = data[6];
this.courseID_FK = Long.parseLong(data[7]);

}

@Override
public void writeToParcel(Parcel dest, int flags) {
// TODO Auto-generated method stub
dest.writeStringArray(new String[] { String.valueOf(this.id),
this.noteDescription, this.noteTitle, this.creationDate,
this.notePic, this.noteAudio, this.noteVideo,
String.valueOf(this.courseID_FK) });

}

public static final Parcelable.Creator<CourseNote> CREATOR = new Creator<CourseNote>() {

@Override
public CourseNote[] newArray(int size) {
// TODO Auto-generated method stub
return new CourseNote[size];
}

@Override
public CourseNote createFromParcel(Parcel source) {
// TODO Auto-generated method stub
return new CourseNote(source);
}
};

@Override
public int describeContents() {
// TODO Auto-generated method stub
return 0;
}

// ---------------------------------------------------------------------------

// I am ignoring setters/getters
}

当用户点击我发送的列表项时 Parcelable反对其他 Activity ,我在 onItemClickListner 中这样做如下(但它工作正常):

        getListView().setOnItemClickListener(new OnItemClickListener() {

@Override
public void onItemClick(AdapterView<?> arg0, View view,
int position, long id) {
long selectedNoteId = id;

mIntent = new Intent(getActivity(), AddEditNoteActivity.class);

mCursor = (Cursor) mNoteListAdapter2.getItem(position);

mTempCourseNote = new CourseNote(
id,
mCursor.getString(mCursor
.getColumnIndex(CourseNote.COL_CONTENT)),
mCursor.getString(mCursor
.getColumnIndex(CourseNote.COL_TITLE)),
String.valueOf(mCursor.getLong(mCursor
.getColumnIndex(CourseNote.COL_CREATION_DATE))),
"", "", "", mCursor.getLong(mCursor
.getColumnIndex(CourseNote.COL_COURSE_ID_FK)));

mIntent.putExtra("method", "edit");
mIntent.putExtra(CourseNote._ID, mTempCourseNote);
mIntent.putExtra(Course._ID, currentCourseID);

Log.i(StudyManagerDataSource.LOG_TAG, "Going to start activity");
startActivity(mIntent);

}
});

这就是我接收 ParcelAble 对象的方式:

CourseNote mTempCourseNote = (CourseNote) getIntent().getParcelableExtra(
CourseNote._ID);

最佳答案

问题出在我在

中使用的 key 的数据类型中

mIntent.putExtra(CourseNote._ID, mTempCourseNote);

它的数据类型是long,我把数据类型改成了string,现在问题解决了!

关于android - Key _id 需要 Parcelable 但值为 java.lang.Long。在 Activity 之间传递 Parceable 对象时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17161074/

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