Singleton构造函数崩溃我的应用程序

时间:2013-09-28 03:14:47

标签: android constructor crash singleton

我正在测试我的应用程序的模型层,我想在列表中添加一个元素。但每当我尝试将一些数据添加到我的数据模型中时,应用程序就会崩溃。我找不到这个的原因。

我的数据模型代码。

public class DataModel {


private List<Log> logs;
private static DataModel instance;
private Context ctx;


//Singleton constructor
private DataModel()
{
   //This makes it crash
   logs.add(new Log("1234","sms", 123545, 1, 0));

   //Load logs from database - Not done yet.

}

public static DataModel getInstance()
{
    if (instance == null)
    {
        //Creates the instance
        instance = new DataModel();
    }
    return instance;
}

我的日志代码

public class Log {


private String phonenumber;
private String type;
private long date;
private int incoming;
private int outgoing;
private long id;

//Constructor for incoming sms or call
public Log( String Phonenumber, String Type, long Date, int Incoming, int Outgoing)
{

    this.phonenumber = Phonenumber;
    this.type = Type;
    this.date = Date;
    this.incoming = Incoming;
    this.outgoing = Outgoing;
}


public long getId()
{
    return id;
}

public void setId(long id)
{
    this.id = id;
}

public String getPhonenumber()
{
    return phonenumber;
}

public void setPhonenumer(String phonenumber)
{
    this.phonenumber = phonenumber;
}

public String getType()
{
    return type;
}

public void setType(String type)
{
    this.type = type;
}

public long getDate()
{
    return date;
}

public void setDate(long date)
{
    this.date = date;
}

public int getIncoming()
{
    return incoming;
}

public void setIncoming(int incoming)
{

    this.incoming = incoming;

}

public int getOutgoing()
{
    return outgoing;
}

public void setOutgoing (int outgoing)
{

    this.outgoing = outgoing;
}

5 个答案:

答案 0 :(得分:2)

您尚未初始化logs。执行此语句时的null

logs.add(new Log("1234","sms", 123545, 1, 0));

变化:

private List<Log> logs;

为:

private List<Log> logs = new ArrayList<Log>();

答案 1 :(得分:0)

我在你的代码中看到了一个上下文,但你没有设置它或在任何地方使用它,所以你可能剥离了部分代码。与此相关,如果你将它用于与UI相关的东西(以及其他一些情况),我可以向你保证,如果你每次屏幕方向改变或你改变活动时没有重置它,它都会使你的应用程序崩溃。

答案 2 :(得分:0)

您尚未实例化列表对象

private List<Log> logs;

将构造函数更新为此

//Singleton constructor
private DataModel()
{
   //This makes it crash
   logs = new ArrayList<Log>();
   logs.add(new Log("1234","sms", 123545, 1, 0));

   //Load logs from database - Not done yet.

}

现在每次调用构造函数时,都会得到一个列表对象的新副本。

答案 3 :(得分:0)

使用前初始化列表

您也可以在构造函数中初始化List

公共类DataModel {

private List<Log> logs= new ArrayList<Log>();
private static DataModel instance;
private Context ctx;


//Singleton constructor
private DataModel()
{
   //This makes it crash
   logs.add(new Log("1234","sms", 123545, 1, 0));


   int i=0;

   //Load logs from database - Not done yet.

}

public static DataModel getInstance()
{
    if (instance == null)
    {
        //Creates the instance
        instance = new DataModel();
    }
    return instance;

}

}

答案 4 :(得分:0)

不要初始化全局日志,也要使用synchronized getInstance方法,这样如果两个线程同时尝试访问,则只能创建一个实例。

使用此代码:

public class DataModel {
    private List<Log> logs;
    private static DataModel instance;
    private Context ctx;


    //Singleton constructor
    private DataModel()
    {
       if(logs == null){
           logs = new ArrayList<Log>();
        }
       logs.add(new Log("1234","sms", 123545, 1, 0));

       //Load logs from database - Not done yet.

    }

    public synchronized static DataModel getInstance()
    {
        if (instance == null)
        {
            //Creates the instance
            instance = new DataModel();
        }
        return instance;
    }