我有一个扩展到SQLiteOpenhelper的功能性DBhelper类,该类成功创建并填充了我的数据库。但是,当我尝试使用复选框值填充数据库时,我的应用程序崩溃了。我相信我的代码片段controller.insert_checkbox(checked)出了点问题,因为当我注释掉它时,我的代码运行并在屏幕上显示吐司。这是我的代码;
公共类LocationCategoryChkbxNew扩展了AppCompatActivity {
Button button2;
CheckBox chkbox15;
int checked = 0;
ResturantsDbHelper controller;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_location_category_chkbx_new);
controller = new ResturantsDbHelper(this);
button2 = (Button)findViewById(R.id.button2);
getSupportActionBar().setTitle("Your Foodometer: Hungry ..");
chkbox15=(CheckBox)findViewById(R.id.checkBox15);
chkbox15.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked){
checked = 1;
}else {
checked = 0;
}
}
});
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
controller.insert_checkbox(checked);
Toast.makeText(getApplicationContext(), "Saved '"+ checked + "' in DB", Toast.LENGTH_SHORT).show();
Intent myIntent = new Intent(LocationCategoryChkbxNew.this,
ResturantCategoryChkboxNew.class);
startActivity(myIntent);
}
});
}
这是我的DBHelper类
公共类ResturantsDbHelper扩展了SQLiteOpenHelper { 私有静态最终字符串TAG =“ DatabaseHelper”;
// FIRST DB - Name of the resturant database file. if you change the DB Schema you must increment the database version
private static final String DATABASE_NAME1 = "Resturants.db";
//Resturant Database version
private static final int DATABASE_VERSION1 = 1;
//import LocationCategoryChkbx class
//private LocationCategoryChkbx;
public ResturantsDbHelper(Context context){
super(context, DATABASE_NAME1,null, DATABASE_VERSION1);
}
@Override
public void onCreate(SQLiteDatabase db) {
String SQL_CREATE_RESTURANTS_TABLE = "CREATE TABLE " + ResturantEntry.TABLE_NAME + "("
+ ResturantEntry._ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
+ ResturantEntry.COLUMN_NAME + " TEXT NOT NULL,"
+ ResturantEntry.COLUMN_TYPE + " TEXT NOT NULL,"
+ ResturantEntry.COLUMN_ADDRESS + " TEXT NOT NULL,"
+ ResturantEntry.COLUMN_LOCATION + " TEXT NOT NULL,"
+ ResturantEntry.COLUMN_GPSCOORDINATES + " INTEGER NOT NULL,"
+ ResturantEntry.COLUMN_TELEPHONE + " INTEGER NOT NULL,"
+ ResturantEntry.COLUMN_OPENINGHOURS + " INTEGER NOT NULL,"
+ ResturantEntry.COLUMN_CLOSINGHOURS + " INTEGER NOT NULL,"
+ ResturantEntry.COLUMN_CHECKBOX + " TEXT);";
db.execSQL(SQL_CREATE_RESTURANTS_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int i, int i1) {
db.execSQL(String.format("DROP IF TABLE EXISTS %s", TABLE_NAME));
onCreate(db);
}
///我们创建了一种向数据库添加数据的方法,我们将其称为“ Insert_data” // public boolean insert_data(String s,String trim,String item,String trim1,String s1,String trim2,String s2,String trim3){ public void insert_data(字符串mResturantName,字符串mAddress,字符串mRestTypeSpinner,字符串mLocationSpinner,字符串mGPS_coordinates,字符串mTelephone,字符串mOpeningHour,字符串mClosingHour){ SQLiteDatabase db = this.getWritableDatabase(); ContentValues contentValues =新的ContentValues(); contentValues.put(ResturantContract.ResturantEntry.COLUMN_NAME,mResturantName); contentValues.put(ResturantContract.ResturantEntry.COLUMN_TYPE,mAddress); contentValues.put(ResturantContract.ResturantEntry.COLUMN_ADDRESS,mRestTypeSpinner); contentValues.put(ResturantContract.ResturantEntry.COLUMN_LOCATION,mLocationSpinner); contentValues.put(ResturantContract.ResturantEntry.COLUMN_GPSCOORDINATES,mGPS_coordinates); contentValues.put(ResturantContract.ResturantEntry.COLUMN_TELEPHONE,mTelephone); contentValues.put(ResturantContract.ResturantEntry.COLUMN_OPENINGHOURS,mOpeningHour); contentValues.put(ResturantContract.ResturantEntry.COLUMN_CLOSINGHOURS,mClosingHour);
this.getWritableDatabase().insertOrThrow(ResturantContract.ResturantEntry.TABLE_NAME,"",contentValues);
}
public Cursor getResturantList(){
SQLiteDatabase database=this.getWritableDatabase();
Cursor resturantdata;
resturantdata = database.rawQuery("SELECT * FROM " + TABLE_NAME,null);
return resturantdata;
}
public void insert_checkbox(int checked) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(ResturantContract.ResturantEntry.COLUMN_CHECKBOX, checked );
this.getWritableDatabase().insertOrThrow(ResturantContract.ResturantEntry.TABLE_NAME,"",contentValues);
}
}
我将不胜感激,我已经为此奋斗了2天