1
我一直在試圖弄清楚一兩個星期,不能做到這一點。爲應用程序存儲HashMaps [CodeName One]
我有兩個類,MyApplication
和Store
。這只是一個測試程序,看看我的存儲代碼有什麼問題。
這是MyApplication
類
public class MyApplication {
private Form current;
private Resources theme;
private Store o;
public void init(Object context) {
theme = UIManager.initFirstTheme("/theme");
Util.register("Store", Store.class);
Toolbar.setGlobalToolbar(true);
}
public void start() {
if(current != null){
current.show();
return;
}
Form hi = new Form("Hi World");
TextField enter = new TextField("","Enter Here", 20, TextField.ANY);
Button add = new Button("Add");
add.addActionListener((ev)->
o.add(enter.getText() + "", 100)); /*Failing here*/
hi.add(enter).add(add);
hi.show();
}
private void save()
{
Storage.getInstance().writeObject("NameOfFile", o);
}
private void load()
{
o = (Store) Storage.getInstance().readObject("NameOfFile");
}
public void stop() {
current = Display.getInstance().getCurrent();
if(current instanceof Dialog) {
((Dialog)current).dispose();
current = Display.getInstance().getCurrent();
}
}
public void destroy() {
}
}
這是Store
類
public class Store implements Externalizable {
private static final int VERSION = 1;
HashMap<String, Integer> data;
public void externalize(DataOutputStream out) throws IOException
{
Util.writeObject(data, out);
}
public void internalize(int version, DataInputStream in) throws IOException
{
data = (HashMap<String, Integer>)Util.readObject(in);
}
public void add(String s, Integer i)
{
data.put(s, i);
}
public int getVersion()
{
return VERSION;
}
public String getObjectId()
{
return "Store";
}
}
我以前用過一個哈希表的指針在MyApplication
類,但它無法在同一位置。