为Android应用程序编写一些测试,并在尝试打开文件时发生故障。通过上下文时,我的预感就是崩溃了。
这是我如何传递上下文
testFactoryQueued.java:
Object obj = Factory.getInstance(getInstrumentation().getContext());
进入
Factory.java
private static Factory mInstance;
private Context mContext;
public static Factory getInstance(Context context) {
if (mInstance == null) {
mInstance = new Factory(context);
}
return mInstance;
}
private Factory(Context context) {
mContext = context;
}
// Try to open file
try {
InputStream is = mContext.getResources().getAssets().open(inFile);
...etc...
} catch (IOException e) {
e.PrintStackTrace();
}
提前致谢!
答案 0 :(得分:0)
设置
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
在你的清单中