目前正在通過Eclipse和JUnit爲Android創建測試框架。我正在實現的最後一件事是配置文件和讀取器,以便在必要時可以使用配置文件來更改各種屬性。我的框架的結構如下:Eclipse Android配置文件總是返回空
MainFramework (project)
Base package
Utility package
Config class
Testing (project)
examples package
Artist testing class
的配置類如下:
公共類配置{
private static Config instance;
public Context context;
public static Properties prop;
public static StorefrontConfig getInstance(Context context) {
if(instance == null)
instance = new StorefrontConfig(context);
return instance;
}
protected StorefrontConfig(Context cont) {
context = cont;
AssetManager manager = context.getAssets();
try {
InputStream instream = manager.open("config");
readConfig(instream);
}
catch (Exception e) {
Log.d("cool", "Failed to create properly initialized config class");
}
}
private static void readConfig(InputStream instream) {
try {
String line = "";
BufferedReader read = new BufferedReader(new InputStreamReader(instream));
while ((line = read.readLine()) != null) {
String[] split_line = line.split("=", 2);
prop.setProperty(split_line[0], split_line[1]);
}
prop.store(new FileOutputStream("config.properties"), "Default and local config files");
read.close();
}
catch (Exception e) {
Log.d("cool", "Failed to create properly initialized config class");
}
}
public String getProperty (String propertyKey) {
try {
return prop.getProperty(propertyKey);
}
catch (Exception e) {
Log.d("cool", "Failed to access property");
return null;
}
}
public Context getContext() {
return context;
}
當我調用的getProperty()方法在我的代碼,它總是返回null。但是,我不知道它是否最初無法讀取和寫入值或發生了什麼。我所知道的是,我的程序使用硬編碼值,但在使用此類並在需要的代碼中通過config.getProperty()引用它時(我的主框架有一個被所有測試繼承的Config類)。
任何幫助將非常感激。唯一能想到的是Java的Properties類不能用於Android?