当我单击按钮将数据写入数据库时,我一直在尝试遵循newBoston教程并发现自己遇到运行时错误。 LogCat显示“无法在活动中找到方法addModuleButton(View)。
有什么建议吗?
添加模块活动
public class AddCourse extends Activity implements OnClickListener {
Button sqlAddModule;
EditText sqlModuleCode, sqlModuleName, sqlModuleType, sqlModuleDay,
sqlModuleStart, sqlModuleEnd, sqlModuleLocation, sqlModuleComments;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.add_course);
sqlAddModule= (Button)findViewById(R.id.addToDatabase);
sqlModuleCode = (EditText)findViewById(R.id.labelEditModuleCode);
sqlModuleName = (EditText)findViewById(R.id.labelEditModuleFull);
sqlModuleType = (EditText)findViewById(R.id.labelCourseType);
sqlModuleDay = (EditText)findViewById(R.id.labelDay);
//sqlModuleStart = (EditText)findViewById(R.id.labelEditModuleCode);
//sqlModuleEnd = (EditText)findViewById(R.id.labelEditModuleCode);
sqlModuleLocation = (EditText)findViewById(R.id.labelEditModuleLocation);
sqlModuleComments = (EditText)findViewById(R.id.labelEditModuleComments);
}
public void onClick (View addModuleButton)
{
boolean didItWork = true;
try{
String moduleCode = sqlModuleCode.getText().toString();
String moduleName = sqlModuleName.getText().toString();
String moduleDay = sqlModuleDay.getText().toString();
String moduleType = sqlModuleType.getText().toString();
String moduleLocation = sqlModuleLocation.getText().toString();
String moduleComments = sqlModuleComments.getText().toString();
database entry = new database(AddCourse.this);
entry.open();
entry.createEntry(moduleCode, moduleName,moduleType, moduleDay,moduleLocation, moduleComments);
entry.close();
}catch(Exception e){
didItWork = false;
}finally{ if(didItWork){
Dialog d = new Dialog(this) ;
d.setTitle("BooYa!");
TextView tv = new TextView(this);
tv.setText("Success");
d.setContentView(tv);
d.show();
}
}
}
}
数据库类
public class database {
public static final String KEY_ROWID = "_id";
public static final String KEY_MODULECODE = "module_code";
public static final String KEY_MODULENAME = "module_name";
public static final String KEY_MODULETYPE = "module_type";
public static final String KEY_MODULEDAY = "module_day";
public static final String KEY_MODULESTART = "module_start";
public static final String KEY_MODULEEND = "module_end";
public static final String KEY_MODULELOCATION = "module_location";
public static final String KEY_MODULECOMMENTS = "module_comments";
private static final String DATABASE_NAME = "module_database";
private static final String DATABASE_TABLE = "my_modules";
private static final int DATABASE_VERSION = 1;
private DbHelper myHelper;
private final Context myContext;
private SQLiteDatabase moduleDatabase;
private static class DbHelper extends SQLiteOpenHelper {
public DbHelper(Context context){
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE " + DATABASE_TABLE + " (" +
KEY_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
KEY_MODULECODE + " TEXT NOT NULL, " +
KEY_MODULENAME + " TEXT NOT NULL, " +
KEY_MODULETYPE + " TEXT NOT NULL, " +
KEY_MODULEDAY + " TEXT NOT NULL, " +
//KEY_MODULESTART + " TEXT, " +
//KEY_MODULEEND + " TEXT, " +
KEY_MODULELOCATION + " TEXT NOT NULL, " +
KEY_MODULECOMMENTS + " TEXT NOT NULL,);"
);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
db.execSQL("DROP TABEL IF EXISTS " + DATABASE_TABLE);
onCreate(db);
}
}
public database(Context c){
myContext = c;
}
public database open()throws SQLException{
myHelper = new DbHelper(myContext);
moduleDatabase = myHelper.getWritableDatabase();
return this;
}
public void close(){
myHelper.close();
}
public long createEntry(String moduleCode, String moduleName, String moduleType,
String moduleDay, String moduleLocation, String moduleComments) {
ContentValues cv = new ContentValues();
cv.put(KEY_MODULECODE,moduleCode);
cv.put(KEY_MODULENAME,moduleName);
cv.put(KEY_MODULETYPE,moduleType);
cv.put(KEY_MODULEDAY,moduleDay);
cv.put(KEY_MODULELOCATION,moduleLocation);
cv.put(KEY_MODULECOMMENTS,moduleComments);
return moduleDatabase.insert(DATABASE_TABLE,null,cv);
// TODO Auto-generated method stub
}
}
答案 0 :(得分:1)
LogCat显示“无法在活动中找到方法addModuleButton(View)。”
您的按钮似乎使用XML属性android:onClick="addModuleButton"
,因此只需更改此内容:
public void onClick (View addModuleButton)
进入这个:
public void addModuleButton (View view)
来自onClick
文档:
此名称必须对应于只接受View类型的一个参数的公共方法。例如,如果指定
android:onClick="sayHello"
,则必须声明上下文的public void sayHello(View v)
方法(通常是您的活动)。
答案 1 :(得分:0)
数据库类
private static class DbHelper extends SQLiteOpenHelper {
public static final String KEY_ROWID = "_id";
public static final String KEY_MODULECODE = "module_code";
public static final String KEY_MODULENAME = "module_name";
public static final String KEY_MODULETYPE = "module_type";
public static final String KEY_MODULEDAY = "module_day";
public static final String KEY_MODULESTART = "module_start";
public static final String KEY_MODULEEND = "module_end";
public static final String KEY_MODULELOCATION = "module_location";
public static final String KEY_MODULECOMMENTS = "module_comments";
private static final String DATABASE_NAME = "module_database";
private static final String DATABASE_TABLE = "my_modules";
private static final int DATABASE_VERSION = 1;
private DbHelper myHelper;
private final Context myContext;
private SQLiteDatabase moduleDatabase;
public DbHelper(Context context){
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE " + DATABASE_TABLE + " (" +
KEY_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
KEY_MODULECODE + " TEXT NOT NULL, " +
KEY_MODULENAME + " TEXT NOT NULL, " +
KEY_MODULETYPE + " TEXT NOT NULL, " +
KEY_MODULEDAY + " TEXT NOT NULL, " +
//KEY_MODULESTART + " TEXT, " +
//KEY_MODULEEND + " TEXT, " +
KEY_MODULELOCATION + " TEXT NOT NULL, " +
KEY_MODULECOMMENTS + " TEXT NOT NULL,);"
);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
db.execSQL("DROP TABEL IF EXISTS " + DATABASE_TABLE);
onCreate(db);
}
}
public database(Context c){
myContext = c;
}
public database open()throws SQLException{
myHelper = new DbHelper(myContext);
moduleDatabase = myHelper.getWritableDatabase();
return this;
}
public void close(){
myHelper.close();
}
public long createEntry(String moduleCode, String moduleName, String moduleType,
String moduleDay, String moduleLocation, String moduleComments) {
ContentValues cv = new ContentValues();
cv.put(KEY_MODULECODE,moduleCode);
cv.put(KEY_MODULENAME,moduleName);
cv.put(KEY_MODULETYPE,moduleType);
cv.put(KEY_MODULEDAY,moduleDay);
cv.put(KEY_MODULELOCATION,moduleLocation);
cv.put(KEY_MODULECOMMENTS,moduleComments);
return moduleDatabase.insert(DATABASE_TABLE,null,cv);
// TODO Auto-generated method stub
}
模块活动
DbHelper myHelper= new DbHelper (this, DATABASENAME, null, VERSION_CODE);
SQLiteDatabase db = myHelper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put("module_code", "XXX");
values.put("module_name", "XXX");
...
db.insert("module_database", "", values);