当我尝试添加数据或查看数据时,我的应用程序崩溃,而不是添加到SQLiteDatabase
MainActivity:
public class MainActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener , DatePickerDialog.OnDateSetListener, TimePickerDialog.OnTimeSetListener {
//Database code
DatabaseHelper myDb;
Button btnAddData, btnViewData;
private Context context;
private Object view;
private EditText Team1;
private EditText Team2;
private Button btnSubmit;
private TextView Date1;
private TextView Time1;
private EditText TeamOne;
private EditText TeamTwo;
private EditText DateOne;
private EditText TimeOne;
private Spinner SportOne;
private EditText LocationOne;
private EditText EmailOne;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
DateOne = (EditText) findViewById(R.id.editText5);
TimeOne = (EditText) findViewById(R.id.editText6);
SportOne = (Spinner) findViewById(R.id.spinner1);
TeamOne = (EditText) findViewById(R.id.editText);
TeamTwo = (EditText) findViewById(R.id.editText2);
LocationOne = (EditText) findViewById(R.id.editText3);
EmailOne = (EditText) findViewById(R.id.editText4);
btnAddData = (Button)findViewById(R.id.ConfirmButton);
btnViewData = (Button)findViewById(R.id.button3);
//Drop down menu
Spinner spinner = findViewById(R.id.spinner1);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.sports, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(this);
AddData();
ViewData();
}
public void AddData() {
btnAddData.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String date = DateOne.getText().toString();
String time = TimeOne.getText().toString();
String team_one = TeamOne.getText().toString();
String team_two = TeamTwo.getText().toString();
String location = LocationOne.getText().toString();
String email = EmailOne.getText().toString();
boolean insertData = myDb.addData(date, time, team_one, team_two, location, email);
if (insertData == true) {
Toast.makeText(MainActivity.this, "Data Successfully Inserted!", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(MainActivity.this, "Something went wrong :(.", Toast.LENGTH_LONG).show();
}
}
});
}
public void ViewData() {
btnViewData.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Cursor data = myDb.showData();
if(data.getCount() == 0) {
display("Error", "No Data Found.");
return;
}
//display message
StringBuffer buffer = new StringBuffer();
while (data.moveToNext()){
buffer.append("ID: " + data.getString(0) + "\n");
buffer.append("Date: " + data.getString(1) + "\n");
buffer.append("Time: " + data.getString(2) + "\n");
buffer.append("Team One: " + data.getString(3) + "\n");
buffer.append("Team Two: " + data.getString(4) + "\n");
buffer.append("Location: " + data.getString(5) + "\n");
buffer.append("Email: " + data.getString(6) + "\n");
}
display("All Stored Data:", buffer.toString());
}
});
}
public void display(String title, String message) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setCancelable(true);
builder.setTitle(title);
builder.setMessage(message);
builder.show();
}
@Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
}
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
}
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
}
DatabaseHelper:
public class DatabaseHelper extends SQLiteOpenHelper {
public static final String DATABASE_NAME = "match.db";
public static final String TABLE_NAME = "match_table";
public static final String COL1 = "ID";
public static final String COL2 = "DATE";
public static final String COL3 = "TIME";
public static final String COL5 = "TEAM_ONE";
public static final String COL6 = "TEAM_TWO";
public static final String COL7 = "LOCATION";
public static final String COL8 = "EMAIL";
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, " +
" DATE TEXT, TIME TEXT, TEAM_ONE TEXT, TEAM_TWO TEXT, LOCATION TEXT, EMAIL TEXT)";
db.execSQL(createTable);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(db);
}
public boolean addData(String date, String time, String team_one,
String team_two, String location, String email){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(COL2, date);
contentValues.put(COL3, time);
contentValues.put(COL5, team_one);
contentValues.put(COL6, team_two);
contentValues.put(COL7, location);
contentValues.put(COL8, email);
long result = db.insert(TABLE_NAME, null, contentValues);
if (result == -1) {
return false;
}
else {
return true;
}
}
public Cursor showData(){
SQLiteDatabase db = this.getWritableDatabase();
Cursor data = db.rawQuery("SELECT * FROM " + TABLE_NAME, null);
return data;
}
}
似乎无法发现我的错误。
您还可以将微调器信息放入SQLite吗?