1

I have this piece of code in Activity A:

Properties properties = new Properties();

/** Fill up properties here */

Intent intent = new Intent(this,Another.class);
intent.putExtra("prop",properties);
startActivity(intent);

Now..I try grant that extra from the Activity B (through Intent's Bundle) by:

Properties properties = (Properties) bundle.getSerializable("prop");

But I getting java.lang.ClassCastException followed by that message:

java.lang.RuntimeException: Unable to start activity ComponentInfo{gr.kanellis.sqlify/gr.kanellis.sqlify.activities.DatabaseView}: java.lang.ClassCastException: java.util.HashMap cannot be cast to java.util.Properties

And pointing the line that I cast the Properties object in Activity B.

I can't figure out how to solve this problem. Any help will be much appreciated.

  • Is `Properties` even `Serializable` ? It would also help to know what information you want to pass. There are several ways, if you want the best answer, be specific. – UDKOX May 22 '16 at 19:17
  • Generally, I want to pass something like a HashMap and Properties object can simply help me out with this. And yes. It is Serializable. –  May 22 '16 at 19:25
  • Is it too much of a hack for you to use a 3rd party library such as EventBus? With it, you can send **any** object from any place in code to another one. – Vucko May 22 '16 at 20:17

2 Answers2

1

The problem is that Properties does not implement Serializable directly - instead inheriting from HashMap (which is serializable).

A simple solution is to simply call the Properties constructor, passing the HashMap to it:

class Properties extends HashMap<String,String> {
     public Properties(HashMap map) {
         super(map);
     }
}

Now, when you deserialize, just call:

Properties properties = new Properties((HashMap)bundle.getSerializable("prop"));

DroidFiddle example here: https://droidfiddle.net/rvcgfcy/2

adelphus
  • 10,116
  • 5
  • 36
  • 46
0

Try this:

Properties properties = new Properties();
prop.setProperty("Hello", "Hi!");
/*...*/
Bundle bundle = new Bundle();
bundle.putExtra("prop", properties);

Then in your activity you can get it back, but you must cast it as a HashMap:

Intent intent = this.getIntent();
Bundle bundle = intent.getExtras();
HashMap<String,String> map= (HashMap<String, String>)bundle.getSerializable("prop");
map.get("Hello")

The reason why this works it's completely explained in this solution and as a summary it's because Properties implements Map, and any class that implements Map that you put into a Bundle will come out as a HashMap.

If you still need an object of type Property you can do this:

Properties properties = new Properties();
properties.putAll(map);
Community
  • 1
  • 1
ACBM
  • 657
  • 1
  • 10
  • 26
  • Well..there is no method name "putSerializable" but just the simple one "putExtra".. –  May 22 '16 at 19:22
  • Are sure? I'm currently testing it, and it has this method. Please note that I'm using the class [Bundle](https://developer.android.com/reference/android/os/Bundle.html#putSerializable%28java.lang.String,%20java.io.Serializable%29), so you should first do "Bundle bundle = new Bundle;" and in that object call the method putSerializable. – ACBM May 22 '16 at 19:26
  • No this is still not working, I getting ClassCastException again :( –  May 22 '16 at 19:33
  • Please read my new answer, as explained in this [post](http://stackoverflow.com/a/12305459/1430615) Android works in a way that any class that implements Map that you put into a Bundle will come out as a HashMap. So you must cast it to HashMap, and if you need it, then change it to Properties. – ACBM May 22 '16 at 19:54
  • Yes that's what I done couple minutes ago and solve my problem! –  May 22 '16 at 21:11