2016-07-27 111 views
0

因此,我試圖構建一個應用程序,用戶創建一個事件並將事件存儲到數據庫中。但每次我點擊保存按鈕,應用程序崩潰。我嘗試了很多事情來試圖破譯這個問題,但我一直在失敗。 下面是該SQLiteOpenHelper當我嘗試將數據插入數據庫時​​,應用程序崩潰

public class AccessDatabase extends SQLiteOpenHelper { 


// Declaring the variables 
private static final String DB_NAME = "unilog"; 
private static final int DB_VERSION = 2; 
private static final String TB_NOTES = "Notes"; 
private static final String NOTE_NAME = "name"; 
private static final String NOTE_DATE = "date"; 
private static final String NOTE_LOCATION = "location"; 
private static final String NOTE_DESCRIPTION = "description"; 
private static final String NOTE_STATUS = "status"; 
private static final String NOTE_CREATION = "createdby"; 


private static final String CREATE_NOTES_TABLE = "CREATE TABLE " + TB_NOTES + " (" 
     + " num INTEGER PRIMARY KEY AUTOINCREMENT, " + NOTE_NAME + " TEXT," 
     + NOTE_DATE + " TEXT," + NOTE_LOCATION + " TEXT," + NOTE_DESCRIPTION + " TEXT," + NOTE_CREATION + " TEXT," + NOTE_STATUS + " INTEGER)"; 

private static final String CREATE_USER_TABLE = "CREATE TABLE USERS (" 
     + "id INTEGER PRIMARY KEY AUTOINCREMENT, " 
     + "NAME TEXT NOT NULL, " 
     + "PASSWORD TEXT NOT NULL, " 
     + "EMAIL TEXT NOT NULL) "; 
//Done Declaring the variables 

AccessDatabase(Context context) { 

    super(context, DB_NAME, null, DB_VERSION); 
} 

private static void insertUser(SQLiteDatabase db, String name, String password, String email) { 
    ContentValues user = new ContentValues(); 
    user.put("NAME", name); 
    user.put("PASSWORD", password); 
    user.put("EMAIL", email); 
    db.insert("USERS", null, user); 


} 

@Override 
public void onCreate(SQLiteDatabase db) { 
    db.execSQL(CREATE_NOTES_TABLE); 
    db.execSQL(CREATE_USER_TABLE); 

    insertUser(db, "Yosr", "123456789", "yosrgouddi[email protected]"); 
    insertUser(db, "Yesmine", "nadalind", "[email protected]"); 

    ContentValues io = new ContentValues(); 
    io.put(NOTE_NAME, "Yosr"); 
    io.put(NOTE_DATE, "121212"); 
    io.put(NOTE_LOCATION, "Unilog"); 
    io.put(NOTE_DESCRIPTION, "Android"); 
    io.put(NOTE_CREATION, "By me"); 
    io.put(NOTE_STATUS, 0); 
    db.insert(TB_NOTES, null, io); 


} 

@Override 
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
    db.execSQL("DROP TABLE IF EXISTS " + TB_NOTES); 
    db.execSQL("DROP TABLE IF EXISTS USERS"); 

    onCreate(db); 
} 


//TOdo add note 
public void addNote(Notes note) { 
    SQLiteDatabase db = this.getWritableDatabase(); 
    ContentValues values = new ContentValues(); 
    values.put(NOTE_NAME, note.getName()); 
    values.put(NOTE_DATE, note.getEventDate()); 
    values.put(NOTE_LOCATION, note.getEventLocation()); 
    values.put(NOTE_DESCRIPTION, note.getEventDescription()); 
    values.put(NOTE_CREATION, note.getCreatedBy()); 
    values.put(NOTE_STATUS, note.getStatus()); 
    //todo insert row 
    db.insert(TB_NOTES, null, values); 
    db.close(); 

} 
//TODO : GETTING A SINGLE NOTE 

public Notes getNote(String name, String date) { 
    SQLiteDatabase db = this.getReadableDatabase(); 
    Cursor cursor = db.query(TB_NOTES, new String[]{NOTE_NAME, NOTE_DATE, NOTE_LOCATION, NOTE_DESCRIPTION, NOTE_CREATION, NOTE_STATUS}, 
      NOTE_NAME + " =? AND " + NOTE_DATE + " =?", new String[]{name, date}, null, null, null, null); 
    if (cursor != null) 
     cursor.moveToFirst(); 
    Notes note = new Notes(cursor.getString(0), cursor.getString(1), cursor.getString(2), cursor.getString(3), cursor.getString(4), cursor.getInt(5)); 
    cursor.close(); 
    return note; 
} 


//TODO : GETTING ALL NOTES 

public List<Notes> getallnotes() { 
    List<Notes> notesList = new ArrayList<Notes>(); 
    String SelectQuery = "SELECT * FROM " + TB_NOTES; 
    SQLiteDatabase db = this.getReadableDatabase(); 
    Cursor cursor = db.rawQuery(SelectQuery, null); 
    if (cursor.moveToFirst()) { 
     do { 
      Notes note = new Notes(); 
      note.setName(cursor.getString(1)); 
      note.setEventDate(cursor.getString(2)); 
      note.setDescription(cursor.getString(4)); 
      note.setEventLocation(cursor.getString(3)); 
      note.setCreatedBy(cursor.getString(5)); 
      notesList.add(note); 
     } while (cursor.moveToNext()); 
    } 
    cursor.close(); 
    return notesList; 
} 

//TODO delete note 

public void DeleteNote(Notes notes) 

{ 
    SQLiteDatabase db = this.getWritableDatabase(); 
    db.delete(TB_NOTES, "(" + NOTE_NAME + " = ? AND " + NOTE_DATE + "= ?)", 
      new String[]{notes.getName(), notes.getEventDate()}); 
    db.close(); 
} 

//Get number of notes 
public int getNotesCount() { 
    String countQuery = "SELECT * FROM " + TB_NOTES; 
    SQLiteDatabase db = this.getReadableDatabase(); 
    Cursor cursor = db.rawQuery("select * from Notes", null); 

    cursor.close(); 

    // return count 
    return cursor.getCount(); 
} 

} 代碼和這裏的note.java

public class Notes { 
private int status; 
private String Name, EventDate,EventLocation,EventDescription , createdBy; 
public Notes() 
{} 

public Notes (String name, int status) 
{ 
    this.Name = name; 
    this.status = status; 

} 
public Notes(String name,String EventDate, String EventLocation, String Description, String createdby, int status) { 

    this.createdBy = createdby; 
    this.EventDescription= Description; 
    this.EventLocation= EventLocation; 
    this.EventDate=EventDate; 
    this.Name = name; 
    this.status = status; 
} 

// setters 

//TODO NAME 
public void setName(String name) { 
    this.Name = name; 
} 
//TODO DESCRIPTION 
public void setDescription (String description) { 
    this.EventDescription = description; 
} 

//TODO LOCATION 
public void setEventLocation (String location) 
{ 
    this.EventLocation = location; 
} 

//TODO STATUS 
public void setStatus(int status) { 
    this.status = status; 
} 
//TODO EVENTDATE 
public void setEventDate(String date) 
{this.EventDate = date;} 

//TODO CREATEDBY 
public void setCreatedBy(String createdby){ 
    this.createdBy = createdby; 
} 

// getters 

//TODO NAME 
public String getName() { 
    return this.Name; 
} 
//TODO STATUS 
public int getStatus() { 
    return this.status; 
} 
//TODO CREATEDBY 

public String getCreatedBy() { 
    return createdBy; 
} 

//TODO EVENTDATE 
public java.lang.String getEventDate() { 
    return EventDate; 
} 
//TODO EVENTLOCATION 
public java.lang.String getEventLocation() { 
    return EventLocation; 
} 

public String getEventDescription() { 
    return EventDescription; 
} 

}

代碼,最後createnote.java

public class CreateNote extends Activity { 

//TODO Declaring Variables 
String name; 

TextView enddate, startdate, datetext; 
Button display, SaveEvent, CancelEvent; 
ImageButton StartDate, EndDate, datebutton; 
EditText eventlocation, eventdescription, newnote; 
Calendar mycalendar1 = Calendar.getInstance(); 
Calendar mycalendar2 = Calendar.getInstance(); 
Calendar mytime = Calendar.getInstance(); 
AccessDatabase db = new AccessDatabase(this); 
//TODO Done declaring variables 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.create_note); 



    //TODO:EditText : location & description 
    eventlocation = (EditText) findViewById(R.id.location); 
    eventdescription = (EditText) findViewById(R.id.description); 
    String location = eventlocation.getText().toString(); 
    String description = eventdescription.getText().toString(); 
    eventlocation.setText(location, TextView.BufferType.EDITABLE); 
    eventdescription.setText(description, TextView.BufferType.EDITABLE); 
    newnote = (EditText)findViewById(R.id.newnote); 
    name = newnote.getText().toString(); 

    //TODO save and cancel buttons 

    SaveEvent = (Button)findViewById(R.id.savenote); 
    CancelEvent = (Button)findViewById(R.id.cancelnote); 
    SaveEvent.setOnClickListener(new View.OnClickListener(){ 
     public void onClick(View v) { 
     // String msg = db.getNote("Yosr", "121212").getCreatedBy(); 

        int non = db.getNotesCount(); 
         StringBuilder stro = new StringBuilder(); 
        stro.append(non); 
        String nin = stro.toString(); 


       Toast.makeText(getApplicationContext(), nin, Toast.LENGTH_LONG).show(); 

      } 
     }); 




      //TODO :Buttons : start & end & date 


    datebutton = (ImageButton) findViewById(R.id.datebutton); 
    StartDate = (ImageButton) findViewById(R.id.dpresultday); 
    EndDate = (ImageButton) findViewById(R.id.dpendtime); 


    //TODO TextView : startdate and enddate 
    datetext = (TextView) findViewById(R.id.date); 
    startdate = (TextView) findViewById(R.id.startdate); 
    enddate = (TextView) findViewById(R.id.enddate); 


    //TODO Calendar and datepicker stuff for start 

    final DatePickerDialog.OnDateSetListener date = new DatePickerDialog.OnDateSetListener() { 
     @Override 
     public void onDateSet(DatePicker view, int year, int month, int day) { 
      mytime.set(Calendar.YEAR, year); 
      mytime.set(Calendar.MONTH, month); 
      mytime.set(Calendar.DAY_OF_MONTH, day); 
      updatelabel2(); 
     } 
    }; 

    //TODO Startdate button listener 
    StartDate.setOnClickListener(new View.OnClickListener() { 
     @Override 

     public void onClick(View view) { 
      int hour = mycalendar1.get(Calendar.HOUR_OF_DAY); 
      int minute = mycalendar2.get(Calendar.MINUTE); 
      TimePickerDialog mtimepicker; 
      mtimepicker = new TimePickerDialog(CreateNote.this, new TimePickerDialog.OnTimeSetListener() { 
       @Override 
       public void onTimeSet(TimePicker timePicker, int selectedhour, int selectedminute) { 

        startdate.setText(selectedhour + ":" + selectedminute); 

       } 
      }, hour, minute, true); 
      mtimepicker.setTitle("selecttime"); 
      mtimepicker.show(); 
     } 

    }); 


    //TODO TimePicker listener for startButton 

    datebutton.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 
      new DatePickerDialog(CreateNote.this, date, mycalendar1 
        .get(Calendar.YEAR), mytime.get(Calendar.MONTH), 
        mytime.get(Calendar.DAY_OF_MONTH)).show(); 
     } 
    }); 

    //TODO Enddate button listener 
    EndDate.setOnClickListener(new View.OnClickListener() { 
     @Override 

     public void onClick(View view) { 
      int hour = mycalendar2.get(Calendar.HOUR_OF_DAY); 
      int minute = mycalendar2.get(Calendar.MINUTE); 
      TimePickerDialog mtimepicker; 
      mtimepicker = new TimePickerDialog(CreateNote.this, new TimePickerDialog.OnTimeSetListener() { 
       @Override 
       public void onTimeSet(TimePicker timePicker, int selectedhour, int selectedminute) { 
        enddate.setText(selectedhour + ":" + selectedminute); 
       } 
      }, hour, minute, true); 
      mtimepicker.setTitle("selecttime"); 
      mtimepicker.show(); 

     } 
    }); 




} 




private void updatelabel2(){ 

    String myFormat = "MM/dd/yy"; //In which you need put here 
    SimpleDateFormat sdf = new SimpleDateFormat(myFormat, Locale.US); 

    datetext.setText(sdf.format(mytime.getTime())); 

} }

+9

添加日誌貓錯誤 –

+4

我們需要看到你的代碼.... – Razgriz

+0

我們想回答這個問題,但首先我們想出來的虛構的世界,分享你的代碼和堆棧跟蹤! – Naveen

回答

0

不能使用封閉光標:

public int getNotesCount() { 
    SQLiteDatabase db = this.getReadableDatabase(); 
    Cursor cursor = db.rawQuery("select * from Notes", null); 
    int count = cursor.getCount() 
    cursor.close(); 

    return count; 
} 
+0

儘管我已將數據插入表中,但我得到「0」作爲響應,可能是什麼問題? – kessi

+0

@kessi爲此創造了另一個問題 – Divers

相關問題