从android中的应用程序上下文获取资产
是否可以使用应用程序实例使用getAssets,如:从android中的应用程序上下文获取资产,android,assets,Android,Assets,是否可以使用应用程序实例使用getAssets,如: public class MyApp extends Application { static private MyApp sInstance = null; public static MyApp Instance() { if( sInstance == null ) { sInstance = new MyApp(); }
public class MyApp extends Application
{
static private MyApp sInstance = null;
public static MyApp Instance()
{
if( sInstance == null )
{
sInstance = new MyApp();
}
return sInstance;
}
}
在功能和使用方面:
*** AssetManager as = MyApp.Instance().getAssets();
Fonts.ARIAL = Typeface.createFromAsset(as, "Arial.ttf" );
舱单:
应用程序名称为“MyApp”
getAssets函数调用返回NullPointerException(*行)
我怎么修理它
我们找到了答案:
MyApp应该是这样的:
public class MyApplication extends Application {
private static MyApplication singleton;
public MyApplication getInstance(){
return singleton;
}
@Override
public void onCreate() {
super.onCreate();
singleton = this;
}
}
请看一下里面:
请确认,1。将MyApp添加到AndroidManifest.xml中,2。您有一个MyApp的静态引用,并在MyApp类的onCreate或构造函数中分配该引用。三。在Instance()方法中返回该值。请查看编辑。我想不应该使用applicationContext中的getAssets()函数?也许它在解决问题上有一些局限性——你给了我正确的方向