2013-09-28 21 views
0

我測試了我的應用程序的模型圖層,並且想要將元素添加到列表中。但是,無論何時我嘗試將一些數據添加到我的數據模型中,應用程序都會崩潰。我找不到原因。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; 
} 

回答

2

您還沒有初始化logs代碼。它null當你執行該語句:

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

變化:

private List<Log> logs; 

到:

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

我看到你的代碼的上下文,但是你不對它進行設置或使用它在任何地方,所以也許你剝離了你的代碼的一部分。與此相關,如果您將它用於與UI相關的內容(以及其他一些情況),我可以向您保證,如果您不在每次屏幕方向更改或您更改活動時都重置它,它會使應用程序崩潰。

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. 

} 

現在每次來。

0

初始化列表中使用

,然後才能在構造函數初始化列表以及

公共類的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; 

}

}

0

不要初始化全局日誌並使用同步的getIns tance方法,以便在兩個線程同時嘗試訪問時只創建一個實例。

使用此代碼:

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