1 回答
TA贡献1784条经验 获得超2个赞
getAssets()需要context预先初始化应用程序,然后才能]调用它。下面的代码流程应该能够实现您的目标:
public class MyContent extends Application {
public static final Map<String, Element> ITEM_MAP;
@Override
public void onCreate() {
super.onCreate();
ITEM_MAP = new HashMap<String, Element>();
AssetManager assetManager = getAssets();
try {
InputStream inputStream = getAssets().open("data.csv");
InputStreamReader inputStreamReader=new InputStreamReader((inputStream));
BufferedReader bufferedReader=new BufferedReader((inputStreamReader));
String tt="";
while ((tt=bufferedReader.readLine())!=null){
MyContent.addItemElement(MyContent.createElement(tt));
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
编辑1
在你的里面AndroidManifest.xml,记得在标签下添加你的应用程序类application,例如
<application
android:name=". MyContent"
...
</application>
添加回答
举报