NOTE: the solution is in the answer starting with SOLUTION that I can't accept since it's my own anwer. I hope this helps someone else.
I want to make my class, that includes a parcelable object, parcelable. In other words let's say I have a class with 3 fields: String name, int id and Location loc. Now I want to make this class Parcelable. How can I do it? So far that's what I did:
public MyClass(Parcel in) {
name = in.readString();
id = in.readInt();
loc = in.readParcelable(Location.class.getClassLoader()); }
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(name);
dest.writeInt(id);
dest.writeParcelable(loc, flags); }
When I go to use it I get and error: Class not found when unmarshalling...
Does anyone know why I get this error and how to fix it?
Thanks in advance for any answer.
EDIT:
I have to add that since I'm sending this data to a service working on his own process I'm using the Handler technique so maybe that could be the problem. But still that method is done well since it was working perfectly until I introduce the Location object in MyClass.
SOLUTION: I found the solution. My implementation was fine. The problem was that I send the message at a separate process so I need to set the proper classLoader in order to make it work right. So the upper code is correct and the thing I had to change was when I go to retrive the object from the bundle or the message. I'll paste the link that gave me the solution even if nobody set it as the correct answer: Problem unmarshalling parcelables
This part of the code seems right.
Make sure that inside Location you do the Parcel stuff correctly as well.
One other thing that could go wrong is, that if you serialized the class before you had the Location in it and now you are trying to de-serialize the old object which doesn't have Location yet.
Related
Edit: Figured it out, check my posted answer if you're having similar issues.
I know there are several questions about this issue, but none of their solutions are working for me.
In my model class I have made sure to use List instead of Arraylist to avoid Firebase issues, but am still getting this error. It's a lot of code but most questions ask for all the code so I'll post it all.
TemplateModelClass.java
//
I've used this basic model successfully many times. For the
HashMaps<String, List<String>>,
the String is an incremented Integer converted to String. The List's are just Strings in a List. Here's some sample JSON from Firebase:
//
Formatted that as best as I could. If you need a picture of it let me know and I'll get a screenshot
And am getting this error, as stated in the title:
com.google.firebase.database.DatabaseException: Expected a Map while deserializing, but got a class java.util.ArrayList
The most upvoted question about this seems to have something to do with a problem using an integer as a key, but I think I've avoided that by always using an integer converted to a string. It may be interpreting it strangely, so I'll try some more stuff in the meantime. Thanks for reading!
Alright, figured it out. If anyone reading this has this problem and are using incremented ints/longs/whatever that get converted to strings, you must add some characters to the converted int. Firebase apparently converts these keys back into non-Strings if it can be converted.
For example, if you do something like this:
int inc = 0;
inc++; // 1
map.put(String.valueOf(inc), someList);
Firebase interprets that key as 1 instead of "1".
So, to force Fb to intepret as a string, do something like this:
int inc = 0;
inc++; // 1
map.put(String.valueOf(inc) + "_key", someList);
And everything works out perfectly. Obviously if you also need to read those Strings back to ints, just split the string with "[_]" and you're good to go.
The main issue is that you are using a List instead of a Map. As your error said, while deserializing it is expectig a Map but is found an ArrayList.
So in order to solve this problem youd need to change all the lists in your model with maps like this:
private Map<String, Object> mMapOne;
After changing all those fileds like this, you need also to change your public setters and getters.
Hope it helps.
I am trying to send bean object(implents Serializable) with 16 strings data obtained from a parser. I am sending that using putExtra("string",serializablevalue) and I'm receiving that using getIntent().getSerializable("string"). I have used this option for almost 10 functionalities it works fine for me.But particular this functionality alone always returns me null in receiving location.I have cross checked it while sending.it has value. While in the receiving location.
My doubt will bean with 16 fields could be sent with this method. Suggest me a better solution for this problem.
Try:
getIntent().getExtras().getSerializable("string")
My code works fine with ArrayList<String>. Could you please give me/us your serializable-strings?
On the other side, based on this: http://developer.android.com/reference/android/os/Parcelable.html, then if you use custom class, you need to "transfer" the data between: public void writeToParcel(Parcel out, int flags) and private MyParcelable(Parcel in).
In details, you can write your data to out, and get them from in.
Check if the String are equals to in both activities!!
it happened to me I had one different capital letter, when I corrected it it worked :)
i have a basic function which requires serializing in my android app. The user will add some values to an ArrayList and i want to serialize it to avoid using a database for this little option and of course TO LEARN how to serialize (i'm a begginer) because it seems useful. Anyways the user save something in the arraylist, the program shuts down, the program starts up again and the user is able to see the saved data. How can i implement this? Can you provide some code snippet or a useful link?
Thanks a lot!!
You can do this by custom bean class and implement Serializable to that
so now when you create ArrayList<E> of that class it is Serializable.
Example:
Class dataBean implements Serializable
{
public String name;
}
ArrayList<dataBean> dataBeanArrayList = new ArrayList();
So dataBeanArrayList is now Serializable and you can also pass this between Intent.
I suggest using flexjson to serialize the data to a file. Then you can read that back using that library. This has several advantages over serialization which is being able to load your stream back into potential differing versions of your objects. Using ObjectInputStream you have to be very careful, and quite frankly I've never seen it work all that well.
http://flexjson.sourceforge.net
Here is a blog post how to do that:
http://wrongnotes.blogspot.com/2010/09/flexjson-meet-android.html
Is there a way to have an global settings variable for an android application, which is accessable as well from any help java classes without giving them context?!
I try to explain what I mean.
I have an application version as string value in strings.xml
I can get its value from every android activity, but not from help java classes withought giving context
What I do now, is saving it in a static variable of my first activity, but it seems, that sometimes it will be erased and set to null.
May be I do something wrong?!
Sorry for newbie question.
And thank you in advance,
Mur
P.s.
I wrote a small tutorial for this topic, to show the solution.
A variable declared as public, static, and final will be visible to all of your classes and never get erased.
public static final String VERSION = "1.2.3.4";
You could make a public static variable in your application class that you fill with the value from strings.xml in the onCreate method. The application class is a singleton and will be the last thing that is killed as part of your app so it will always be there and if you make it public static there will be only one instance.
I'm guessing that you have a JAVA class for some common utility functions. You get the value of your string using a context in your Activity/Service and then pass in that value to the JAVA class function as a parameter.
hi i have found Uri as immutable reference i dont know what it is the exact meaning of immutable reference... can anyone help me?
It's a variable that cannot be changed once set. Very useful when you have multithreaded code since being able to change a variable's value might be a source of many hard to find problems in your code.
If it's immutable, it's usually good.
A good example of an immutable class within the .NET Framework is System.String. Once you create a String object, you can’t ever change it. There’s no way around it; that’s the way the class is designed. You can create copies, and those copies can be modified forms of the original, but you simply cannot change the original instance for as long as it lives, without resorting to unsafe code. If you understand that, you’re probably starting to get the gist of where I’m going here: For a referencebased object to be passed into a method, such that the client can be guaranteed that it won’t change during the method call, it must itself be immutable.
In a world such as the CLR where objects are held by reference by default, this notion of immutability becomes very important. Let’s suppose that System.String was mutable, and let’s suppose you could write a method such as the following fictitious method:
public void PrintString( string theString )
{
// Assuming following line does not create a new
// instance of String but modifies theString
theString += ": there, I printed it!";
Console.WriteLine( theString );
}
Imagine the callers’ dismay when they get further along in the code that called this method and now their string has this extra stuff appended onto the end of it. That’s what could happen if System. String were mutable. You can see that String’s immutability exists for a reason, and maybe you should consider adding the same capability to your design.
EX: string is immutable...
if u have for ex string s =" whatever" and u output it with uppercase letter..for ex
Console.Write(s.ToUpper())the console will print u WHATEVER...but the string s will still be whatever... unlike the mutable type which will change the string from whatever to WHATEVER
"immutable" means "can't change the value"
"mutable" == "changeable"
"immutable" == "not changeable"
In java , every thing is treated as String and object , Now try to think that if have created a program of 10000 lines and in this there you have added "public" 100 times so do you think that every time this public is created in storage . else what we can do , we can created something like that when ever we find something like this we will fetch it from there there ( String pool )