如何将 Properties 对象从一个意图传递到另一个意图?
How to pass Properties object from an intent to another?
我有这段代码在Activity A:
Properties properties = new Properties();
/** Fill up properties here */
Intent intent = new Intent(this,Another.class);
intent.putExtra("prop",properties);
startActivity(intent);
现在..我尝试通过以下方式从 Activity B(通过 Intent's Bundle)授予额外的费用:
Properties properties = (Properties) bundle.getSerializable("prop");
但我收到 java.lang.ClassCastException 后跟该消息:
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
并指向我在 Activity B 中投射 Properties 对象的线。
我不知道如何解决这个问题。
任何帮助将不胜感激。
试试这个:
Properties properties = new Properties();
prop.setProperty("Hello", "Hi!");
/*...*/
Bundle bundle = new Bundle();
bundle.putExtra("prop", properties);
然后在你的 activity 中你可以取回它,但你必须将它转换为 HashMap
:
Intent intent = this.getIntent();
Bundle bundle = intent.getExtras();
HashMap<String,String> map= (HashMap<String, String>)bundle.getSerializable("prop");
map.get("Hello")
为什么这行得通,它在 solution 中得到了完整的解释,作为总结,这是因为 Properties
实现了 Map
,以及任何实现 Map 的 class放入 Bundle 中后会变成 HashMap
.
如果您仍然需要 Property
类型的对象,您可以这样做:
Properties properties = new Properties();
properties.putAll(map);
问题是 Properties 没有直接实现 Serializable - 而是继承自 HashMap(可序列化)。
一个简单的解决方案是简单地调用 Properties 构造函数,将 HashMap 传递给它:
class Properties extends HashMap<String,String> {
public Properties(HashMap map) {
super(map);
}
}
现在,反序列化时,只需调用:
Properties properties = new Properties((HashMap)bundle.getSerializable("prop"));
此处为 DroidFiddle 示例:https://droidfiddle.net/rvcgfcy/2
我有这段代码在Activity A:
Properties properties = new Properties();
/** Fill up properties here */
Intent intent = new Intent(this,Another.class);
intent.putExtra("prop",properties);
startActivity(intent);
现在..我尝试通过以下方式从 Activity B(通过 Intent's Bundle)授予额外的费用:
Properties properties = (Properties) bundle.getSerializable("prop");
但我收到 java.lang.ClassCastException 后跟该消息:
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
并指向我在 Activity B 中投射 Properties 对象的线。
我不知道如何解决这个问题。 任何帮助将不胜感激。
试试这个:
Properties properties = new Properties();
prop.setProperty("Hello", "Hi!");
/*...*/
Bundle bundle = new Bundle();
bundle.putExtra("prop", properties);
然后在你的 activity 中你可以取回它,但你必须将它转换为 HashMap
:
Intent intent = this.getIntent();
Bundle bundle = intent.getExtras();
HashMap<String,String> map= (HashMap<String, String>)bundle.getSerializable("prop");
map.get("Hello")
为什么这行得通,它在 solution 中得到了完整的解释,作为总结,这是因为 Properties
实现了 Map
,以及任何实现 Map 的 class放入 Bundle 中后会变成 HashMap
.
如果您仍然需要 Property
类型的对象,您可以这样做:
Properties properties = new Properties();
properties.putAll(map);
问题是 Properties 没有直接实现 Serializable - 而是继承自 HashMap(可序列化)。
一个简单的解决方案是简单地调用 Properties 构造函数,将 HashMap 传递给它:
class Properties extends HashMap<String,String> {
public Properties(HashMap map) {
super(map);
}
}
现在,反序列化时,只需调用:
Properties properties = new Properties((HashMap)bundle.getSerializable("prop"));
此处为 DroidFiddle 示例:https://droidfiddle.net/rvcgfcy/2