获取资产();来自另一个班级 [英] getAssets(); from another class
本文介绍了获取资产();来自另一个班级的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个简单的读取 txt 文件的功能.
I have a simple read a txt-file function.
AssetManager mngr = getAssets();
InputStream is = mngr.open("textdb.txt");
它适用于我的主要活动.但如果我在单独的类中使用相同的代码,getAssets() 只会返回 null/崩溃.
It works from my main activity. But if I use the same code in a separate class, getAssets() just return null / crash.
我不知道为什么它只能在主类中工作.
I am unable to find why it only works from the main class.
有什么想法吗?
解决方案:
subClass.ReadSettings(getApplicationContext());
public String[] ReadSettings(Context myContext) {
}
推荐答案
你的另一个班级也是 Activity
吗?getAssets() 是 Context 的一个方法.如果您的类不是活动,则需要将上下文传递给它,然后在其上调用 getAssets.
Is your other class also an Activity
?
getAssets() is a method of Context. If your class is not an activity, you'll need to pass a context into it and then call getAssets on that.
像这样:
public myClass(Context myContext) {
AssetManager mngr = myContext.getAssets();
InputStream is = mngr.open("textdb.txt");
}
这篇关于获取资产();来自另一个班级的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文