【发布时间】:2011-10-26 04:59:29
【问题描述】:
我正在尝试开发一个使用 appinfo 类来存储设置的应用程序。这是可以持久的。当我使用新安装的应用程序时,它运行良好。然后,如果我重新加载 cod,任何持久性存储访问尝试都会抛出 java.lang.error 并声称我没有权限(在进入应用程序内部之前暂停应用程序权限异常)。
要让我的应用再次运行,我必须清理模拟器并重新启动它。
编辑:忘了提一下,只要我设法打开它,并在应用程序当前在模拟器中打开时重新加载代码,它就可以继续工作。
Edit2:它开始看起来像 6.0 问题。因为如果我在 7.0 模拟器中打开它,一切都很好。
应用信息:
package ca.dftr.lib.persistables;
import java.util.Hashtable;
import net.rim.device.api.system.ApplicationDescriptor;
import net.rim.device.api.util.ContentProtectedHashtable;
import net.rim.device.api.util.Persistable;
/**
* Basic class for storing application specific information.
* Information such as application settings or whether the license agreement was accepted.
* For more complex and specific classes they should be implemented separately and implement persistable
* @author deforbes
*/
public class AppInfo extends ContentProtectedHashtable implements Persistable {
private String _appName = null;
private String _version = null;
/**
* Constructs the application info, creates and persists a hashtable for application settings.
* @param uniqueHexAppIdentifier Can be automatically created in resource class (BUNDLE_ID) or generated using other unique information.
*/
public AppInfo() {
ApplicationDescriptor appDesc = ApplicationDescriptor.currentApplicationDescriptor();
_appName = appDesc.getName();
_version = appDesc.getVersion();
}
/**
* Get the Name of the application
* @return The application name from the app descriptor
*/
public String getName()
{
return _appName;
}
/**
* Get the Version of the application
* @return The application version from the app descriptor
*/
public String getVersion()
{
return _version;
}
}
持久存储助手:
package ca.dftr.main;
import ca.dftr.lib.persistables.AppInfo;
import net.rim.device.api.system.PersistentObject;
import net.rim.device.api.system.PersistentStore;
/**
* Persistant store helper class.
* Thanks to Max Gontar of Stack Overflow
* @author deforbes
*/
public class PersistentStoreHelper{
static PersistentObject appInfoStore = PersistentStore
.getPersistentObject( Global.GUID );
public static void saveAppInfo( AppInfo appInfo ){
saveObject( appInfoStore, appInfo );
}
public static AppInfo retrieveAppInfo(){
return ( AppInfo )retrieveObject( appInfoStore );
}
public static void saveObject( PersistentObject store, Object object ){
synchronized( store ){
store.setContents( object );
store.commit();
}
}
public static Object retrieveObject( PersistentObject store ){
Object result = null;
synchronized( store ){
result = store.getContents();
}
return result;
}
}
将 app info 变量放入我使用它的类中。
public static AppInfo AppData;
static{
AppData = PersistentStoreHelper.retrieveAppInfo();
if (AppData == null){
AppData = new AppInfo();
PersistentStoreHelper.saveAppInfo(AppData);
}
}
在“main”函数中创建应用程序后我的程序的第一行。
Object acceptedLicense = Global.AppData.get("isLicenseAccepted");
if (acceptedLicense == null){
Global.AppData.put("isLicenseAccepted", new Boolean(false));
acceptedLicense = Global.AppData.get("isLicenseAccepted");
}
if (!((Boolean) acceptedLicense).booleanValue()){
//..... ETC. It crashes before this
任何帮助将不胜感激。这是模拟器问题还是我的问题?
【问题讨论】:
标签: blackberry store persistent