1

尝试从资产目录加载文件以将其用作配置文件。AssetManager 找不到它。config.global 和 config.properties 都在项目的 assets 目录中。配置类在 setUp() 函数中生成,然后调用 readConfig 开始。这是主文件中的代码:

protected void setUp() throws Exception {
    super.setUp();
    mLithium = (LithiumExtension) Trio.getInstance(new LithiumExtension(this))
            .getExtension();
    config = StorefrontConfig.getInstance();
    Resources resources = getActivity().getResources();
    AssetManager asM = resources.getAssets();
    InputStream in = asM.open("config.global");
    OutputStream out = asM.openFd("config.properties").createOutputStream();
    config.readConfig(in, out);
    setTestSettings();
}

以及 Config 的代码:

public class Config {

private static Config instance;
public Properties prop;

static {
    instance = new Config();
}

private Config () {
    prop = new Properties();
}

public void readConfig(InputStream in, OutputStream out) {

    try {

        prop.load(in);
        in.close();

        prop.store(out, "yay");
        out.close();
    }
    catch (Exception e) {
        System.out.println("Failed to create properties");
        System.exit(3);
        Log.d("Config", "Failed to create properly initialized config class");
    }
}

public static Config getInstance() {

    if(instance == null)
        instance = new Config();
    return instance;
}

public String getProperty (String key) {
    return prop.getProperty(key);
}
}

跑步时,我得到一个java.io.FileNotFoundExceptionfor config.global。这是在asM.open("config.global")代码行抛出的。一直坚持这个。任何帮助,将不胜感激!

4

1 回答 1

2

在内部接收上下文setUp(Context c)并使用它来获取资产

所以尝试 改变

 AssetManager asM = resources.getAssets();

AssetManager asM = c.getAssets();
于 2013-07-16T18:30:55.423 回答