I have a class classOne which extends parceble. I'm Using classOne class to parse json lets say feeds message json Here is my code
public class classOne implements Parcelable{
public String createdAt = null;
public String description = null;
public String ownerName = null;
public ArrayList<classOne> repliesArrayList = null;
public classOne(JSONObject jObject){
createdAt = jObject.isNull("created_at") == true ? "" :jObject.optString("created_at");
description = jObject.isNull("description") == true ? "": jObject.optString("description");
ownerName = jObject.isNull("name") == true ? "" : jObject.optString("name");
if (!jobject.isNull("messages")) {
repliesArrayList = new ArrayList<classOne>();
JSONArray repliesJsonArray = jobject.optJSONArray("messages");
int count = repliesJsonArray.length() - 1;
for (int repliesCount = count; repliesCount >= 0; repliesCount--) {
classOne repliesMessageModel = new classOne(repliesJsonArray.optJSONObject(repliesCount));
repliesArrayList.add(repliesMessageModel);
}
}
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(createdAt);
dest.writeString(description);
dest.writeString(ownerName);
dest.writeTypedList(repliesArrayList);
}
private void readFromParcel(Parcel in) {
createdAt = in.readString();
description = in.readString();
ownerName = in.readString();
in.readTypedList(repliesArrayList, classOne.CREATOR);
}
public static final Parcelable.Creator<classOne> CREATOR = new Parcelable.Creator() {
public MessageDataModel createFromParcel(Parcel in) {
return new classOne(in);
}
public MessageDataModel[] newArray(int size) {
return new classOne[size]; }
};
}
It's working fine but i'm getting an error when i'm trying to send repliesArrayList from one activity to other activity
Here is my code to send array list from one class to other
Intent intent7 = new Intent(getApplicationContext(), ParceActivity.class);
intent7.putParcelableArrayListExtra("msg", replyList);
startActivity(intent7);
public class ParceActivity extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
System.out.println("----------msg|" +getIntent().getParcelableArrayListExtra("msg").size());
}
}
I'm getting following error.
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
E/AndroidRuntime( 8432): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
E/AndroidRuntime( 8432): at android.app.ActivityThread.access$600(ActivityThread.java:141)
E/AndroidRuntime( 8432): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
E/AndroidRuntime( 8432): at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime( 8432): at android.os.Looper.loop(Looper.java:137)
E/AndroidRuntime( 8432): at android.app.ActivityThread.main(ActivityThread.java:5041)
E/AndroidRuntime( 8432): at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime( 8432): at java.lang.reflect.Method.invoke(Method.java:511)
E/AndroidRuntime( 8432): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
E/AndroidRuntime( 8432): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
E/AndroidRuntime( 8432): at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime( 8432): Caused by: java.lang.NullPointerException
E/AndroidRuntime( 8432): at android.os.Parcel.readListInternal(Parcel.java:2237)
E/AndroidRuntime( 8432): at android.os.Parcel.readList(Parcel.java:1531)
Its working properly if i send any other data but gave me an error if i send replayList. I have no clue what went wrong i have debug this code, value was there while sending replaylist from activity one to other activity. It is because replay list have same data type "classOne"???
Remove repliesArrayList from readToParcel and from writeToParcel.