I have a class called Weekly Result
which implements Parcel interface. I am having a problem retrieving the parcel object when I pass it to another activity.
This is my weeklyResult
class:
public class WeeklyTopicResult implements Parcelable {
public String ID;
public MultiLingual Title = new MultiLingual();
public MultiLingual Text = new MultiLingual();
public String ImageUrl;
public String PublishOn;
public Bitmap ImageSource;
public WeeklyTopicResult(Parcel in) {
// Reads the obj value
ReadFromParcel(in);
}
public WeeklyTopicResult() {
// TODO Auto-generated constructor stub
}
@Override
public int describeContents() {
// TODO Auto-generated method stub
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
// TODO Auto-generated method stub
Log.v("WeekData", "WriteToParcel..." + flags);
dest.writeString(ID);
dest.writeString(ImageUrl);
dest.writeString(PublishOn);
dest.writeParcelable(Title, flags);
dest.writeParcelable(Text, flags);
dest.writeParcelable(ImageSource, flags);
}
public void ReadFromParcel(Parcel obj) {
Log.v("WeeklyClaas",
"ParcelData(Parcel source): time to put back parcel data");
ID = obj.readString();
Title = (MultiLingual) obj.readParcelable(MultiLingual.class
.getClassLoader());
Text = (MultiLingual) obj.readParcelable(MultiLingual.class
.getClassLoader());
ImageUrl = obj.readString();
PublishOn = obj.readString();
ImageSource = (Bitmap) obj
.readParcelable(Bitmap.class.getClassLoader());
}
public class WeeklyTopicResultParcelCreater implements
Parcelable.Creator<WeeklyTopicResult> {
@Override
public WeeklyTopicResult createFromParcel(Parcel source) {
// TODO Auto-generated method stub
return new WeeklyTopicResult(source);
}
@Override
public WeeklyTopicResult[] newArray(int size) {
// TODO Auto-generated method stub
return new WeeklyTopicResult[size];
}
}
}
This is my Multilinqual
class:
public class MultiLingual implements Parcelable {
public String ArbicValue;
public String EnglishValue;
public MultiLingual(Parcel in) {
ReadFromParcel(in);
}
public MultiLingual() {
// TODO Auto-generated constructor stub
}
@Override
public int describeContents() {
// TODO Auto-generated method stub
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
// TODO Auto-generated method stub
dest.writeString(ArbicValue);
dest.writeString(EnglishValue);
}
private void ReadFromParcel(Parcel Obj) {
ArbicValue = Obj.readString();
EnglishValue = Obj.readString();
}
public class MyMultiLingualParcelCreator implements
Parcelable.Creator<MultiLingual> {
@Override
public MultiLingual createFromParcel(Parcel source) {
// TODO Auto-generated method stub
return new MultiLingual(source);
}
@Override
public MultiLingual[] newArray(int size) {
// TODO Auto-generated method stub
return new MultiLingual[size];
}
}
}
And here is where I am sending the object which intent:
public void GoToWeeklyTopic(View v) {
Intent intent = new Intent();
intent.setClass(HomeActivity.this, Topic_of_the_week.class);
Bundle b = new Bundle();
WeeklyTopicResult w = weeks;
// Puts the weeklyresult parcel class into bundle
b.putParcelable("WeeklyContent", w);
intent.putExtras(b);
startActivity(intent);
}
Here's the activity class where I am trying to receive my parcel object:
ImageView weekImg;
TextView title;
TextView ContentText;
WeeklyTopicResult week= new WeeklyTopicResult();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_topic_of_the_week);
Bundle extras = getIntent().getExtras();
//Here I am receiving an error
week =extras.getParcelable("WeeklyContent");
weekImg = (ImageView) findViewById(R.id.imageView1);
title = (TextView) findViewById(R.id.txt_MainTitle);
ContentText = (TextView) findViewById(R.id.txt_page_content_topicweek);
title.setText(week.Title.EnglishValue);
ContentText.setText(week.Text.EnglishValue);
weekImg.setImageBitmap(week.ImageSource);
// String FullPath = RetrieveData_Preference("WeeklyTopicUrl") + Url;
// getImage(FullPath);
}
I even tried to cast the object back but still I am having an error. Logcat error showed me this error but I already implemented a creater class for weeklyresult
LogCat error:
FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activityComponentInfo{com.example.laysapp/com.example.laysapp.AdminPage.Topic_of_the_week}:
android.os.BadParcelableException: Parcelable protocol requires a Parcelable.Creator object called CREATOR on class com.example.laysapp.ParsingLogic.WeeklyTopicResult
E/AndroidRuntime(1231):
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
at android.app.ActivityThread.access$600(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137) at android.app.ActivityThread.main(ActivityThread.java:5039) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:511) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) at dalvik.system.NativeStart.main(Native Method) Caused by: android.os.BadParcelableException: Parcelable protocol requires a Parcelable.Creator object called CREATOR on class com.example.laysapp.ParsingLogic.WeeklyTopicResult at android.os.Parcel.readParcelable(Parcel.java:2086) at android.os.Parcel.readValue(Parcel.java:1965) at android.os.Parcel.readMapInternal(Parcel.java:2226) at android.os.Bundle.unparcel(Bundle.java:223) at android.os.Bundle.getParcelable(Bundle.java:1165) at com.example.laysapp.AdminPage.Topic_of_the_week.onCreate( Topic_of_the_week.java:40) at android.app.Activity.performCreate(Activity.java:5104) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144) ... 11 more
What am I doing wrong here?
From the documentation:
Classes implementing the Parcelable interface must also have a static field called CREATOR, which is an object implementing the Parcelable.Creator interface.
So add this field to your MultiLingual
class:
public static final MyMultiLingualParcelCreator CREATOR = new MyMultiLingualParcelCreator();