有谁能告诉我在哪里实例化像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>>>();
}
}
答案 0 :(得分:1)
这实际上不是真正的Singelton(see Wikipedia for an accurate Description of Singeltons),而是一个普通的静态变量。 如果你想使用更多的静态变量,你可以考虑使用它,因为它使事情变得更加美好。 但是,要回答您的具体问题,您可以使用静态块来初始化这些变量,也可以在变换后直接初始化它们。
public class SharedVariables extends Application
{
public static ArrayList<ArrayList<Map<String,String>>> ArrayOfImageUrlDicts = new ArrayList<ArrayList<Map<String,String>>>();
public static ArrayList<ArrayList<Map<String,String>>> ArrayOfImageNameDicts = new ArrayList<ArrayList<Map<String,String>>>();
@Override
public void onCreate()
{
super.onCreate();
}
}
或
public class SharedVariables extends Application
{
public static ArrayList<ArrayList<Map<String,String>>> ArrayOfImageUrlDicts;
public static ArrayList<ArrayList<Map<String,String>>> ArrayOfImageNameDicts;
static
{
ArrayOfImageUrlDicts = new ArrayList<ArrayList<Map<String,String>>>();
ArrayOfImageNameDicts = = new ArrayList<ArrayList<Map<String,String>>>();
}
@Override
public void onCreate()
{
super.onCreate();
}
}
答案 1 :(得分:0)
public class SharedVariables extends Application
{
ModelSingleTonclass obj;
public static ArrayList<ArrayList<Map<String,String>>> ArrayOfImageUrlDicts;
public static ArrayList<ArrayList<Map<String,String>>> ArrayOfImageNameDicts;
@Override
public void onCreate()
{
super.onCreate();
obj=ModelSingleTonclass.getInstance();
ArrayOfImageUrlDicts = new ArrayList<ArrayList<Map<String,String>>>();
ArrayOfImageNameDicts = new ArrayList<ArrayList<Map<String,String>>>();
}
}