尝试将日期选择器/时间选择器中的选定日期和时间插入SQLite。发生错误NewMeetingActivity.AddData(NewMeetingActivity.java:156)-布尔型insertData = myDB.addData(newDate,newTime);
我目前将选择的日期和时间保存到文本视图中。然后如何将它们添加到数据库中?我尝试过将它们转换为字符串,但是我认为在应用崩溃时即时通讯功能无法正常运行
public class NewMeetingActivity extends AppCompatActivity implements DatePickerDialog.OnDateSetListener, TimePickerDialog.OnTimeSetListener {
private Button btn_date, btn_time, btn_confirm_meeting, btn_back_to_meetings, btn_location;
private CalendarView calendarView;
private TextView myDate, myTime;
DataBaseHelper myDB;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_meetings);
myDate = findViewById(R.id.myDate);
myTime = findViewById(R.id.myTime);
btn_date = findViewById(R.id.button_add_Date);
btn_time = findViewById(R.id.button_add_Time);
btn_confirm_meeting = findViewById(R.id.button_confirm_meeting);
btn_back_to_meetings = findViewById(R.id.button_back_to_meetings);
btn_back_to_meetings.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(NewMeetingActivity.this,
MeetingActivity.class);
startActivity(intent);
}
});
btn_date.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
DialogFragment datePicker = new DateFragment();
datePicker.show(getSupportFragmentManager(), "date picker");
}
});
btn_time.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
DialogFragment timePicker = new TimeFragment();
timePicker.show(getSupportFragmentManager(), "time picker");
}
});
btn_confirm_meeting.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String newDate = myDate.getText().toString();
String newTime = myTime.getText().toString();
if (myDate.length() == 0) {
Toast.makeText(NewMeetingActivity.this, "Enter a date for your meeting", Toast.LENGTH_LONG).show();
}
if (myTime.length() == 0) {
Toast.makeText(NewMeetingActivity.this, "Enter a time for your meeting", Toast.LENGTH_LONG).show();
} else if (myDate.length() != 0 && myTime.length() != 0) {
AddData(newDate, newTime);
myDate.setText("");
myTime.setText("");
}
}
});
}
public void AddData(String newDate, String newTime) {
boolean insertData = myDB.addData(newDate, newTime);
if (insertData == true) {
Toast.makeText(NewMeetingActivity.this, "New Meeting Created!", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(NewMeetingActivity.this, "Something went wrong, Try again", Toast.LENGTH_LONG).show();
}
}
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
Calendar c = Calendar.getInstance();
c.set(Calendar.YEAR, year);
c.set(Calendar.MONTH, month);
c.set(Calendar.DAY_OF_MONTH, dayOfMonth);
String currentDateString =
DateFormat.getDateInstance(DateFormat.FULL).format(c.getTime());
TextView textView = findViewById(R.id.myDate);
textView.setText(currentDateString);
}
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
TextView textView1 = findViewById(R.id.myTime);
textView1.setText("Hour: " + hourOfDay + "Minute: " + minute);
}
}
这是addData在我的数据库类中
public boolean addData(String item1, String item2) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(COL2, item1);
contentValues.put(COL3, item2);
long result = db.insert(TABLE_NAME, null, contentValues);
//if date as inserted incorrectly it will return -1
if (result == -1) {
return false;
} else {
return true;
}
}
database creation
public static final String DATABASE_NAME = "mylist.db";
public static final String TABLE_NAME = "mylist_data";
public static final String COL1 = "ID";
public static final String COL2 = "ITEM1";
public static final String COL3 = "ITEM2";
public DataBaseHelper(Context context) {
super(context, DATABASE_NAME, null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
String createTable = "CREATE TABLE " + TABLE_NAME + " (ID INTEGER PRIMARY
KEY
AUTOINCREMENT, " +
" ITEM1 TEXT, " + "ITEM2 TEXT)";
db.execSQL(create)}
答案 0 :(得分:0)
读取错误:
Attempt to invoke virtual method 'boolean C/.myapplication.DataBaseHelper.addData(java.lang.String, java.lang.String)' on a null object reference
这意味着您正在尝试对一个为null的对象调用方法addData
。在这种情况下,它是以下行:
boolean insertData = myDB.addData(newDate, newTime);
myDb
永远不会在您的代码中实例化,因此尝试在其上调用方法肯定是行不通的。