I am writing an android application and I need to have two classes use the same KeyguardLock object but I am experiencing extreme difficulty in sharing (via serialization) that object. I have tried using the serialization stackoverflow example link but that didn't work at all. I get a "not serializable" IO exception trying to save the object. I have also tried using JSONObject.
Any ideas? Has anyone run into a similar problem?
Why are you trying to serialize it? A object can only be serialize if it implements Serializable which KeyguardLock doesn't.
If you're trying to pass it around Activities, either create a custom Application object and store it there. Or use a public static variable in a class and access it via that. The static variable is probably the better option for this.
Related
I want to instantiate an object in my MainActivity class that will be used and altered by my other activities.
The object is a custom data type.
Would it best best to declare the object as public static in MainActivity and then just use MainActivity.object throughout my application? Or would it be better to just pass the object around using intents?
The main thing I'm worried about using static is that I've seen people saying you use it when you care about privacy but I'm not too sure what that means so.. hoping to get some input.
Thanks
If you need one object on all app, you can store this object in Application class
I want to use a 3rd party library(Wiremock) with Spark. However, I get the following exception:
Caused by: java.io.NotSerializableException:com.github.tomakehurst.wiremock.WireMockServer
Serialization stack:
- object not serializable(class:com.github.tomakehurst.wiremock.WireMockServer, value: com.github.tomakehurst.wiremock.WireMockServer#51813065)
Is there a general way to deal with this?
There are few options:
Kryo might be able to serialize these objects out of the box, depending what’s inside them. Try turning it on as described at http://spark.apache.org/docs/latest/tuning.html
If that doesn’t work, you can create your own “wrapper” objects that implement Serializable, or even a subclass of com.github.tomakehurst.wiremock.WireMockServer. No need to change the original library.
If the library has its own serialization functions, you could also use those inside a wrapper object. Take a look at https://github.com/apache/spark/blob/master/core/src/main/scala/org/apache/spark/SerializableWritable.scala for an example where Spark make Hadoop’s Writables serializable.
You can make the field that is not serializable transient. Then it will just not be serialized.
private transient WireMockServer wireMockServer;
I have a class, Savable that serialize the entire class. It was quick easy and great. Unfortunately, this is a problem if I want to update my app. If I make any changes to Saveable, or its descendants, when an instance is deserialized from an older version, there are virtual, and or abstract errors and other errors thrown.
Reading the Android saving data page here, developers are supposed to use one of the ways listed. As I am building an alarm app, I have opted for the SQLLite option.
The problem, until now, my app has been created with my Saveable class in mind. Switching to a database will require a full reworking of my Alarm class because Alarm has other descendants of Saveable in it. Therefore, to save each alarm, I will need a database for every sub-class of Saveable.
Currently, I have a method, Saveable.save(Context context) that serializes the object to fill. This will require a rewrite because now instead of serializing the entire Alarm class, I need to save the core Alarm stuff the Alarm.db, the subclasses of Saveable into subclass.db.
This presents another issue now with loading. I have a method, Saveable.LOAD(File path), which deserializes a Saveable object, that can be cast to it's original class. Now, since the Alarm class contains other Saveable objects that need to be saved in separate DBs, the Alarm class will need a reference to the each sub-saveable class in alarm.
Needless to say, this becomes explosively messy, quickly. I am not opposed to the work (programming is amazing), but before I short change myself again, is this the way you would solve this problem?
I'm a newer programmer, but can't you get a serialization code for updating that allows an object/class to used with an updated version by going into the command line and putting say: serialver class name and it spits out the static final longserialVersionUID. Then make a static final long serialVersionUID = (insert random long number the command line gives here) in the class you want to serialize and then use with an updated version of the program. You probably already know this, and may not be the answer you are looking for, but me being new its the only way I have heard of serializing a class, updating the source code, then deserializing a class and using it with the updated code.
I am currently working on a videogame, and i want to have the user be able to save their character to a new file. I know how to use the file io (for the most part), but i have been using the 'serialize' to serialize a whole object (that contains all the variables for the character) and save it to a file. The problem is that i am constantly updating the object and making changes to it, so when i try to load the old character with the new object, it errors and crashes. Same with levels as-well (an object holding a few 2d-array of variables).
There must be a better way to do this so it is compatible with future versions. If there is a way, would anybody please offer some source code and/or a link to a nice tutorial? All help is appreciated, thanks!!!
Use XML or an embedded database (fast and lightweight) such as Derby or H2. You could even use a plain old properties file.
In fact, see if the properties file will work for you. And only if that won't work, try XML or the embedded database approach.
if you are looking for java serializers here is the benchmark for you https://github.com/eishay/jvm-serializers/wiki/
Apache Avro seems to perform well.
Another way is to store the values in the persistent store like HSQLDB or H2 db and load it to memory at startup and persist when needed.You can also use SQLite (for driver check this)
You can implement Externalizable instead of Serializable, and in the readExternal() and writeExternal() methods you can put the logic to read/write the object. This way you have full control of serialization/deserialization and can make changes fairly easily. Alternatively you can use JSON serialization by using Gson. I would not recommend XML, but if you want to you can check out xstream for the same thing.
If you are extending your objects in backwards compatible ways, i.e. add fields, and not removing fields. Make sure that you have declared a serialVersionUID as per the serializable javadoc.
http://download.oracle.com/javase/1.5.0/docs/api/java/io/Serializable.html
One additional option to consider since you're already using serialization, you could implement Externalizable instead of Serializable. The code you use to serialize objects would remain the same. However in your class you would specify exactly how you want it serialized by overriding readExternal() and writeExternal(). E.g.:
public class MyClass implements Externalizable {
private int foo;
private String bar;
public readExternal(ObjectInput in) {
foo = in.readInt();
bar = in.readUTF();
}
public writeExternal(ObjectOutput out) {
out.writeInt(foo);
out.writeUTF(bar);
}
}
Just be sure to keep the order the same when reading and writing. Try to only add fields, however if you need to remove a field leave a gap to account for old versions.
Ultimately though if you're making a lot of changes it might best to switch to a properties or XML file as LES2 suggested. It'll be more portable and readable that way.
This game uses java.util.prefs.Preferences for cross-platform convenience. Because keys are stored individually, new additions rarely interfere with existing entries.
I got a question: Is it possible to get a list of currently instantiated objects from the VM?
I am using a framework and try to implement an event handler (Hibernate, Interceptor). My problem now is that I need a Properties file during the execution of this handler. I cannot pass a reference to the Interceptor class, because Hibernate or the interface does not have such a method.
I now thought, why not trying to get the reference another way? Or is it possible to register global available objects during the runtime of an application?
Thanks for advice and regards from Germany,
Marco
I now thought, why not trying to get
the reference another way? Or is it
possible to register global available
objects during the runtime of an
application?
You can use a public static field. This belongs to the class rather than to any instance of the class. Very simple example:
public class Properties {
public static String PROPERTY = "abc";
}
You will then be able to access it from anywhere with:
Properties.PROPERTY
You can find more here:
http://java.sun.com/docs/books/tutorial/java/javaOO/classvars.html