NotSerializable exception with intent.addExtra(parcelable Object) - java

I'm attempting to transfer one object containing an ArrayList of other Objects to another Activity in Android. The idea is to add those to an ArrayList and display them so that the Task in question can be performed.
Task ta = new Task(n, o, t, v, subList);
Intent i = new Intent(this, ActivityList.class);
i.putExtra("task", ta);
startActivity(i);
This is the intent. Task is Parcelable, and the subList is an ArrayList of class Subtask which is also Parcelable.
Surely, since ArrayList always implement Serializable, Parcelling them shouldn't be a problem? The constructor arguments are: String, byte, byte, byte, ArrayList. The bytes are to be used as booleans.
Here is the Parcel code for Task if you need it:
#Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(taskName);
dest.writeByte((byte) (soundCueOne ? 1 : 0));
dest.writeByte((byte) (soundCueTwo ? 1 : 0));
dest.writeByte((byte) (vibrCue ? 1 : 0));
dest.writeSerializable(myList);
}
private Task(Parcel in) {
this.taskName = in.readString();
this.soundCueOne = in.readByte() != 0;
this.soundCueTwo = in.readByte() != 0;
this.vibrCue = in.readByte() != 0;
this.myList = (ArrayList<Subtask>) in.readSerializable();
}
public static final Parcelable.Creator CREATOR = new Parcelable.Creator() {
public Task createFromParcel(Parcel in) {
return new Task(in);
}
public Task[] newArray(int size) {
return new Task[size];
}
};
Can anyone see what is wrong with the code? It's obviously somewhere, maybe even in the Subtask class, since the emulator crashes as soon as the Task is constructed and it tries to Parcel it.

If I see correctly, then the problem is that you are trying to use the ArrayList<T> as a Serializable, even though it is not a Serializable - it is a Parcelable.
Therefore, replace
dest.writeSerializable(myList);
with
dest.writeTypedList(myList);
and replace
this.myList = (ArrayList<Subtask>) in.readSerializable();
with
this.myList = in.readTypedList(new ArrayList<Subtask>(), Subtask.CREATOR);

Related

Sane way to go from ArrayList<Long> through an Intent

I'm currently writing an app that pulls an array of longs from a server via json, and then passes that list from one activity into another. The basic skeleton looks like this:
public void onResponse(Map result)
{
ArrayList handles= (ArrayList)result.get("fileHandles");
Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
intent.putExtra("handles", handles);
}
So the first problem becomes evident, the only methods for putExtra are putIntegerArrayListExtra, putStringArrayListExtra, putCharSequenceArrayListExtra, and putParcelableArrayListExtra. Thinking Long would probably be parcelable, I was wrong it doesn't work (even if I use ArrayList<Long>). Next I thought I'd just pass a long [], however I see no straight-forward conversion to go from ArrayList<Long> to long [] that intent.putExtra will accept. This was the solution I finally ended up with:
ArrayList handles= (ArrayList)result.get("fileHandles");
long [] handleArray = new long[handles.size()];
for (int i = 0; i < handles.size(); i++)
{
handleArray[i] = Long.parseLong(handles.get(i).toString());
}
Obviously this seemed a little bit ridiculous to me, but every other conversion I tried seemed to complain for one reason or another. I've thought about re-thinking my serialization to have the problem taken care of before I get to this point, but I find it odd that passing ArrayList<Long> from activity to activity could be so difficult. Is there a more obvious solution I'm missing?
You can use it as a Serializable extra. ArrayList is Serializable and Long extends Number which also implements Serializable:
// Put as Serializable
i.putExtra("handles", handles);
// Unfortunately you'll get an unsafe cast warning here, but it's safe to use
ArrayList<Long> handles = (ArrayList<Long>) i.getSerializableExtra("handles");
Two options: Use Parcelable or Serializable
in:
Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
intent.putExtra("handles", new FileHandles(handles));
out:
FileHandles handles = intent.getParcelableExtra("handles");
object:
import android.os.Parcel;
import android.os.Parcelable;
import java.util.ArrayList;
import java.util.List;
public class FileHandles implements Parcelable {
private final List<Long> fileHandles;
public FileHandles(List<Long> fileHandles) {
this.fileHandles = fileHandles;
}
public FileHandles(Parcel in) {
int size = in.readInt();
long[] parcelFileHandles = new long[size];
in.readLongArray(parcelFileHandles);
this.fileHandles = toObjects(size, parcelFileHandles);
}
private List<Long> toObjects(int size, long[] parcelFileHandles) {
List<Long> primitiveConv = new ArrayList<Long>();
for (int i = 0; i < size; i++) {
primitiveConv.add(parcelFileHandles[i]);
}
return primitiveConv;
}
public List<Long> asList() { // Prefer you didn't use this method & added domain login here, but stackoverflow can only teach so much..
return fileHandles;
}
#Override
public int describeContents() {
return 0;
}
#Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(fileHandles.size());
dest.writeLongArray(toPrimitives(fileHandles));
}
private static long[] toPrimitives(List<Long> list) {
return toPrimitives(list.toArray(new Long[list.size()]));
}
public static long[] toPrimitives(Long... objects) {
long[] primitives = new long[objects.length];
for (int i = 0; i < objects.length; i++)
primitives[i] = objects[i];
return primitives;
}
public static final Parcelable.Creator CREATOR = new Parcelable.Creator() {
#Override
public FileHandles createFromParcel(Parcel in) {
return new FileHandles(in);
}
#Override
public FileHandles[] newArray(int size) {
return new FileHandles[size];
}
};
}
Serializable (forced to use ArrayList which implements Serializable (List does not)
in:
Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
intent.putExtra("handles", new ArrayList<Long>());
out:
ArrayList handles = (ArrayList) intent.getSerializableExtra("handles");

pass Object to another activity

I have found this way to do this
Student student = new Student (18,"Z r");
Intent i = new Intent(this, B.class);
i.putExtra("studentObject", student);
startActivity(i);
The problem is that if the object changed in the first activity No change took place in the another activity.
I thought how to make it like a constructor that no copy of the object is pass but the object it self.
Thanks
How about if you configure the "object" as a singleton of the entire application? This way, everybody (your app) sees the changes... See some insights here: http://developer.android.com/guide/faq/framework.html#3
For example, in some other file (Student.java):
public class Student {
public String Name;
}
Create a custom application class:
public MyApp extends Application {
private Student obj = new Student();
public Student getMyObject() {
return obj;
}
}
Anywhere in your application (e.g. SomeActivity.java):
Student appStudent = ((MyApp) getActivity().getApplicationContext()).getMyObject();
appStudent.Name = "New Name"; // "global" update
You could also look into a BroadcastReceiver. With a BroadcastReceiver you can send a message from one Activity to another and with an interface you can pass the object from one Activity to the other.
I think this is a great example, where a BroadcastReceiver is created to check the internet connection of the device. But you can easily convert this in a BroadcastReceiver with your own custom action to send the object.
Implement parcelable in your student class and you can copy the student into the intent.
How can I make my custom objects Parcelable?
Code works with parcelable classes
> Student student = new Student (18,"Zar E Ahmer"); Intent i = new
> Intent(this, B.class); i.putExtra("studentObject", student);
> startActivity(i);
Below is an example of bean class I use that implements parcelable. Here you would replace KmlMarkerOptions with Student
#SuppressLint("ParcelCreator")
public class KmlMarkerOptions implements Parcelable {
public MarkerOptions markeroptions = new MarkerOptions();
public String href = "";
public int hrefhash =-1;
public String id = "";
public long imageId = -1;
public int locationId = -1;
public int markerSize = -1;
public KmlMarkerOptions(){
}
public KmlMarkerOptions(Parcel in) {
this.markeroptions = in.readParcelable(null);
this.href = in.readString();
this.hrefhash = in.readInt();
this.id = in.readString();
this.imageId = in.readLong();
this.locationId = in.readInt();
this.markerSize = in.readInt();
}
#SuppressWarnings("rawtypes")
public static final Parcelable.Creator CREATOR = new Parcelable.Creator() {
public KmlSummary createFromParcel(Parcel in) {
return new KmlSummary(in);
}
public KmlSummary[] newArray(int size) {
return new KmlSummary[size];
}
};
#Override
public int describeContents() {
return 0;
}
#Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeParcelable(markeroptions, 0);
dest.writeString(href);
dest.writeInt(hrefhash);
dest.writeString(id);
dest.writeLong(imageId);
dest.writeInt(locationId);
dest.writeInt(markerSize);
}
}

Callback function pointer jna

So i have native function in C declared as int NgSetEvent (int event, long callback). long Callback represent the callback function pointer. Callback prototype is declared as int oncodeline(int code, int documentid, char *string). The callback should be registered with method NgSetEvent. The problem is how to get pointer to that function which should be long or Nativelong? I have tried a lot of different aproaches but none of them gave a result. Callback was never invoked.
I have tried to do like it says in Turn a Callback into a Pointer in JNA, but with no success. i don't know what to try anymore.
Any help will be appreciated.
OnCodeline
public interface OnCodeline extends Callback {
int oncodeline (int code, int documentid, byte[] string);
}
OnCodelineStruct
public class OnCodelineStruct extends Structure {
public OnCodeline onc;
#Override
protected List getFieldOrder() {
return Arrays.asList(new String[] { "onc" });
}
}
Main class
OnCodelineStruct onCodelinStruct;
onCodelinStruct = new OnCodelineStruct();
onCodelinStruct.onc = new OnCodeline() {
#Override
public int oncodeline(int code, int documentid, byte[] string) {
System.out.println("This IS a CALLBACK!");
return 0;
}
};
sbDll.NgSetEvent(0, onCodelinStruct.getPointer().getNativeLong(0));

How to pass Parcelable array to fragment and Activity

I'm working with custom parcelable class. I need to pass this class as array to another activity and new fragments. I googled, but I found arrayList but not arrays. Please mind that I need to pass arrays not Array lists. How to achieve that? Please mind again, performance is a big issue. So any solutions with less performance consumption is very welcome.
Any helps would be very appreciated.
You can pass your arrayList as below snipts.
TestFragment testFragment = new TestFragment();
Bundle args = new Bundle();
args.putSerializable("parsedData", (Serializable)mTestModelList);
testFragment.setArguments(args);
And Get these parcable data in Model as like below:
mTestModelList = (List<TestModel>)getArguments().get("parsedData");
YES, Serialisation is slow bit than Parcelable.
And you can implement that using parceler
Of course, the ParcelWrapper can be added to an Android Bundle to transfer from Activity to Activity using this:
Bundle bundle = new Bundle();
bundle.putParcelable("example", Parcels.wrap(example));
And dereferenced in the onCreate() method:
Example example = Parcels.unwrap(getIntent().getParcelableExtra("example"));
You can get more detail of performance related to Parcelable vs Serializable here.
Here is the write answer
// write parcelable array
final Bundle arguments = new Bundle();
MyParcelable[] myArray = new MyParcelable[10];
arguments.putParcelableArray("key"myArray);
// read parcelable array
MyParcelable[] myArray = (MyParcelable[])getArguments().getParcelableArray("key");
use like following class of your parcealble array
public class ParcelableLaptop implements Parcelable {
private Laptop laptop;
public Laptop getLaptop() {
return laptop;
}
public ParcelableLaptop(Laptop laptop) {
super();
this.laptop = laptop;
}
private ParcelableLaptop(Parcel in) {
laptop = new Laptop();
laptop.setId(in.readInt());
laptop.setBrand(in.readString());
laptop.setPrice(in.readDouble());
laptop.setImageBitmap((Bitmap) in.readParcelable(Bitmap.class
.getClassLoader()));
}
/*
* you can use hashCode() here.
*/
#Override
public int describeContents() {
return 0;
}
/*
* Actual object Serialization/flattening happens here. You need to
* individually Parcel each property of your object.
*/
#Override
public void writeToParcel(Parcel parcel, int flags) {
parcel.writeInt(laptop.getId());
parcel.writeString(laptop.getBrand());
parcel.writeDouble(laptop.getPrice());
parcel.writeParcelable(laptop.getImageBitmap(),
PARCELABLE_WRITE_RETURN_VALUE);
}
/*
* Parcelable interface must also have a static field called CREATOR,
* which is an object implementing the Parcelable.Creator interface.
* Used to un-marshal or de-serialize object from Parcel.
*/
public static final Parcelable.Creator<ParcelableLaptop> CREATOR =
new Parcelable.Creator<ParcelableLaptop>() {
public ParcelableLaptop createFromParcel(Parcel in) {
return new ParcelableLaptop(in);
}
public ParcelableLaptop[] newArray(int size) {
return new ParcelableLaptop[size];
}
};
}
now make Arraylist of ParcelableLaptop and put in the bUndle with argument
intent.putParcelableArrayListExtra("laptop", parcelableLaptopArray);
Where parcelableLaptop is your Arraylist of the Model. And to get the List:
Intent intent = getIntent();
ArrayList<ParcelableLaptop> parcelableLaptop = (ParcelableLaptop) intent
.getParcelableArrayListExtra("laptop");

NullPointerException trying to read Parcel String[]

I am getting a NullPointerException when I am trying to read back a String[] when I create an object from Parcel. Here is my code:
#Override
public void writeToParcel(Parcel out, int flags) {
out.writeInt(floors);
out.writeStringArray(indoorMaps);
}
public static final Parcelable.Creator<Building> CREATOR
= new Parcelable.Creator<Building>() {
public Building createFromParcel(Parcel in) {
return new Building(in);
}
public Building[] newArray(int size) {
return new Building[size];
}
};
private Building(Parcel in) {
floors = in.readInt();
in.readStringArray(indoorMaps);
}
So indoorMaps is an attribute of my class, and it a String[], but I get the NullPointerException. I have checked the dev's documentation but there's nothing there.
I followed this tutorial and they are using readStringArray in there.
Any suggestions? Thanks
You are giving Parcel a null array when calling readStringArray. For it to work, you would have to initialize indoorMaps. You probably want createStringArray instead.

Categories