1

I am trying to store the registration state of the user in my app. i have used Persistent store. But i am not getting the data bak from the store. I have written the below code:

package com.saiservices.util;

import java.util.Enumeration;

import com.saiservices.ui.Logger;


import net.rim.device.api.system.PersistentObject;
import net.rim.device.api.system.PersistentStore;
import net.rim.device.api.util.Persistable;

public class SaiPersistentStore implements Persistable{

    private SaiHashTable mElements = null;
    static SaiPersistentStore instance;
    private static final long KEY = xxxxxxxxxxxx;
    private PersistentObject persistentObject = null;


    /**
     * Gets the single instance of DataBase.
     * 
     * @return single instance of DataBase
     */
    public static SaiPersistentStore getInstance() {
        try {
            if (instance == null) {
                instance = new SaiPersistentStore();
            }
        } catch (Exception e) {
            instance = null;
        }
        return instance;
    }


    private SaiPersistentStore()
    {
         Logger.out("PersistentStoreInfo", "inside constructor  ");
        persistentObject = PersistentStore.getPersistentObject(KEY);
         synchronized (persistentObject) 
           { 
               if (persistentObject.getContents() == null) 
               { 
                   persistentObject.setContents(new SaiHashTable()); 
                   persistentObject.commit(); 
               }  
           }

         mElements = (SaiHashTable)persistentObject.getContents();
         if (mElements == null){
             Logger.out("PersistentStoreInfo", "************"+(mElements == null));
             mElements = new SaiHashTable();
         }
    }


    public String getElement(String key)
    {
        Logger.out("PersistentStoreInfo", "getElement :"+key);
        Logger.out("PersistentStoreInfo", "getElement value   :"+mElements.get(key));

        return (String) mElements.get(key);
    }

    public void setElement(String key, String value)
    {
        Logger.out("PersistentStoreInfo", "setElement11111 :"+key + "   "+value);
        mElements.put(key, value);
        persistentObject.setContents(mElements);
        Logger.out("PersistentStoreInfo", "setElement22222 :"+key+"   "+value);
        persistentObject.commit(); 
    }

    public Enumeration getAllKeys() 
    {
        return mElements.keys();
    }

}

And here is the hashtable code:

package com.saiservices.util;

import java.util.Hashtable;

import net.rim.device.api.util.Persistable;

public class SaiHashTable extends Hashtable implements Persistable{

    public SaiHashTable(){
        super();
    }
    public SaiHashTable(int initialCepacity){
        super(initialCepacity);
    }

}

Now i am setting the element like this:

SaiPersistentStore.getInstance().setElement("Registration","on");

But when i am trying to get the element in theis way, gettting null value:

 SaiPersistentStore.getInstance().getElement("Register")

Where i am doing wrong here? Please help.

4

1 回答 1

3

从方法中删除以下行setElement()

    persistentObject.setContents(mElements);

通过这样做,您将覆盖持久对象的内容,该对象之前是 SaiHashTable 对象。所以下次你尝试转换持久对象的内容时,你会得到一个异常,你会捕获然后重置实例。

此外,您应该尝试获得您设置的相同值:

SaiPersistentStore.getInstance().getElement("Registration")

于 2013-05-15T13:37:37.317 回答