在Android中实例化单例类对象
谁能告诉我在哪里实例化像 ArrayList 这样的单例类对象?
我正在尝试在 onCreate()
方法中实例化 但它没有实例化。谁能告诉我分配到哪里?
public class SharedVariables extends Application
{
public static ArrayList<ArrayList<Map<String,String>>> ArrayOfImageUrlDicts;
public static ArrayList<ArrayList<Map<String,String>>> ArrayOfImageNameDicts;
@Override
public void onCreate()
{
super.onCreate();
ArrayOfImageUrlDicts = new ArrayList<ArrayList<Map<String,String>>>();
ArrayOfImageNameDicts = new ArrayList<ArrayList<Map<String,String>>>();
}
}
Can anyone tell me where to instantiate a singleton class object like ArrayList?
I am trying to instantiate in the onCreate()
method
but its not instantiated. Can any one tell me where to allocate it?
public class SharedVariables extends Application
{
public static ArrayList<ArrayList<Map<String,String>>> ArrayOfImageUrlDicts;
public static ArrayList<ArrayList<Map<String,String>>> ArrayOfImageNameDicts;
@Override
public void onCreate()
{
super.onCreate();
ArrayOfImageUrlDicts = new ArrayList<ArrayList<Map<String,String>>>();
ArrayOfImageNameDicts = new ArrayList<ArrayList<Map<String,String>>>();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这实际上不是真正的辛格尔顿(有关辛格尔顿的准确描述,请参阅维基百科),而是正常的静态多变的。
如果您想使用更多静态变量,您可以考虑使用它,因为它使事情变得更加美观。
但是,要回答您的具体问题,您可以使用静态块来初始化这些变量,或者在减速后直接初始化它们。
或者
This is actually not a real Singelton (see Wikipedia for an accurate Description of Singeltons) but a normal static variable.
If you want to usemore static variables you may consider using this as it makes things much more beautiful.
However to answer your concrete question you can either use a static block where you initialize those variables or initialize them directly after decleration.
or