我刚刚启动一个Android Studio项目,而IVE创建了一个小玩家数据库,我一直在尝试将播放器详细信息添加到数据库中。但是,每次程序都以空指针异常崩溃。一个快速的调试表明,问题是在ongreate方法中呼叫OnClickListener的问题。代码需要进行哪些更改?谢谢
public class DatabaseHelper {
// Declare instance of DBHelper class, context and SQLiteDatabase
private DBHelper ourDbHelper;
private final Context ourContext;
private SQLiteDatabase myDb;
// Initialise the database and table names and version
private static final String DATABASE_NAME = "Club";
private static final String PLAYER_TABLE_NAME = "Players";
//Initialise the PLAYER_TABLE_NAME field names
private static final String PLAYER_ID = "player_id";
private static final String PLAYER_SURNAME = "player_surname";
private static final String PLAYER_FORENAME = "player_forename";
private static final String PLAYER_AGE = "player_age";
private static final String PLAYER_DOB = "player_dob";
private static class DBHelper extends SQLiteOpenHelper {
// This constructor takes the context, db name and db version as
parameters
// and applies them to the DBHelper class
public DBHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
// This constructor takes context as parameter and assigns it to
variable
@Override
public void onCreate(SQLiteDatabase db) {
//Executes the following queries to create the Player Table
db.execSQL("CREATE TABLE " + PLAYER_TABLE_NAME + " (" + PLAYER_ID
+ " INTEGER PRIMARY KEY AUTOINCREMENT, "
+ PLAYER_FORENAME + "TEXT NOT NULL, " + PLAYER_SURNAME +
"TEXT NOT NULL, " +
PLAYER_AGE + " INTEGER NOT NULL, " + PLAYER_DOB + "DATE
NOT NULL );");
}//OnCreate
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int
newVersion) {
// if table is upgraded or already exists
db.execSQL("DROP TABLE IF EXISTS " + PLAYER_TABLE_NAME);
onCreate(db);
}//OnUpgrade
}//DBHelperClass
// Constructor takes a context as a parameter and assigns it to
ourContext
public DatabaseHelper (Context c){
ourContext = c;
}//constructor
// Open writable database and allow to be modified
public DatabaseHelper open() throws SQLException {
ourDbHelper = new DBHelper(ourContext);
myDb = ourDbHelper.getWritableDatabase();
return this;
}//open
//method to close the db to modification
public void close() {
ourDbHelper.close();
}//close
// method to create an entry to the Player table with player attributes as parameters
public long createEntry (String forename, String surname, String age, String dob){
//contentValues will hold the information to be added to the database
ContentValues contentValues = new ContentValues();
//Using put method, insert the parameters into the fields in the table
contentValues.put(PLAYER_FORENAME, forename);
contentValues.put(PLAYER_SURNAME, surname);
contentValues.put(PLAYER_AGE,age);
contentValues.put(PLAYER_DOB, String.valueOf(dob));
//insert the values into the player table
//assign to variable result to check if data added correctly
return myDb.insert(PLAYER_TABLE_NAME,null,contentValues);
}//createEntry
这是AddPlayer Activity类
public class AddPlayer extends Activity implements View.OnClickListener {
//declare variables
Button confirmAdd;
EditText editSurname, editForename, editAge, editDOB;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.add_player_activity);
//Initialise variables
editForename = (EditText) findViewById(R.id.editPlayerForename);
editSurname = (EditText) findViewById(R.id.editPlayerSurname);
editAge = (EditText) findViewById(R.id.editPlayerAge);
editDOB = (EditText) findViewById(R.id.editPlayerDOB);
confirmAdd = (Button) findViewById(R.id.addPlayerBtn);
//set onclicklisteners
confirmAdd.setOnClickListener(this);
}
@Override
public void onClick(View v) {
String forename = editForename.getText().toString();
String surname = editSurname.getText().toString();
String age = editAge.getText().toString();
String dob = editDOB.getText().toString();
DatabaseHelper myDb = new DatabaseHelper(AddPlayer.this);
myDb.open();
myDb.createEntry(forename, surname, age, dob);
Toast.makeText(AddPlayer.this, "Data Inserted", Toast.LENGTH_LONG).show();
}
更改此
DatabaseHelper myDb = new DatabaseHelper(AddPlayer.this);
myDb.open()
DatabaseHelper myDb = new DatabaseHelper(this.getContext());
myDb.open();