0

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

我的数据模型代码。

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;
}
4

5 回答 5

2

您没有初始化logs. 当null您执行此语句时:

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

改变:

private List<Log> logs;

到:

private List<Log> logs = new ArrayList<Log>();
于 2013-09-28T03:18:47.367 回答
0

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

于 2013-09-28T03:23:12.973 回答
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.

}

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

于 2013-09-28T03:27:54.010 回答
0

使用前初始化列表

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

公共类数据模型 {

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;

}

}

于 2013-09-28T04:16:06.140 回答
0

不要初始化全局日志,也不要使用同步的 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;
    }
于 2013-09-28T04:31:22.563 回答