我正在为大学项目创建一个运动团队的android应用程序,我的数据库有点麻烦。我正在尝试向数据库添加一个fixture,但是当我点击“添加”时,我得到以下nullpointer异常。按钮。
12-27 16:40:29.420 6977-6977/com.example.myacer.clubhub E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.NullPointerException
at com.example.myacer.clubhub.Manager.UpdateSchedule$1.onClick(UpdateSchedule.java:54)
at android.view.View.performClick(View.java:4262)
at android.view.View$PerformClick.run(View.java:17421)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4944)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
at dalvik.system.NativeStart.main(Native Method)
错误位于UpdateSchedule类的第54行。第54行是
dbHelper.insertFixture
以下是我的UpdateSchedule类的其余部分,团队经理可以添加夹具
public class UpdateSchedule extends AppCompatActivity {
DBHelper dbHelper;
EditText match_date;
EditText match_time;
EditText match_opponent;
EditText match_venue;
Button btnAdd;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_updateschedule);
match_date = (EditText) findViewById(R.id.fixtureDateEdit);
match_time = (EditText) findViewById(R.id.fixtureTimeEdit);
match_opponent = (EditText) findViewById(R.id.fixtureOpponentEdit);
match_venue = (EditText) findViewById(R.id.fixtureVenueEdit);
btnAdd = (Button) findViewById(R.id.addFixtureButton);
addFixture();
}
public void addFixture() {
btnAdd.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
dbHelper.insertFixture(
match_date.getText().toString(),
match_time.getText().toString(),
match_opponent.getText().toString(),
match_venue.getText().toString());
}
}
);
}
}
这是我的DBHelper类。
public class DBHelper extends SQLiteOpenHelper {
private static final String LOG = "DatabaseHelper";
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "clubhub.db";
private static final String TABLE_FIXTURES = "fixtures";
private static final String TABLE_DIARY = "diary";
private static final String TABLE_NOTIFICATION = "notification";
private static final String TABLE_USERS = "users";
//common column names
private static final String KEY_ID = "id";
private static final String PLAYER_NAME = "player_name";
//fixtures table columns
private static final String MATCH_DATE = "match_date";
private static final String MATCH_TIME = "match_time";
private static final String MATCH_OPPONENT = "match_opponent";
private static final String MATCH_VENUE = "match_venue";
//diary table columns
private static final String SLEEP_LENGTH = "sleep_length";
private static final String SLEEP_QUALITY = "sleep_quality";
private static final String ENERGY = "energy";
private static final String MOOD = "mood";
private static final String APPETITE = "appetite";
private static final String WATER_INTAKE = "water_intake";
private static final String SORENESS = "soreness";
private static final String WORKOUT_TYPE = "workout_type";
private static final String WORKOUT_LENGTH = "workout_length";
private static final String WORKOUT_RPE = "workout_rpe";
//notification table columns
private static final String MESSAGE = "message";
//user table columns
private static final String PASSWORD = "password";
//table create statements
//table fixtures create
private static final String CREATE_TABLE_FIXTURES = "CREATE TABLE " + TABLE_FIXTURES
+ "(" + KEY_ID + " INTEGER PRIMARY KEY,"
+ MATCH_DATE + " DATETIME,"
+ MATCH_TIME + " DATETIME NOT NULL,"
+ MATCH_OPPONENT + " TEXT,"
+ MATCH_VENUE + " TEXT"
+ ")";
//table diary create
private static final String CREATE_TABLE_DIARY = "CREATE TABLE " + TABLE_DIARY
+ "(" + KEY_ID + " INTEGER PRIMARY KEY,"
+ SLEEP_LENGTH + " INTEGER NOT NULL,"
+ SLEEP_QUALITY + " INTEGER NOT NULL,"
+ ENERGY + " INTEGER NOT NULL,"
+ MOOD + " INTEGER NOT NULL,"
+ APPETITE + " INTEGER NOT NULL,"
+ WATER_INTAKE + " INTEGER NOT NULL,"
+ SORENESS + " INTEGER NOT NULL,"
+ WORKOUT_TYPE + " TEXT NOT NULL,"
+ WORKOUT_LENGTH + " DATETIME NOT NULL,"
+ WORKOUT_RPE + " INTEGER NOT NULL"
+ ")";
//table notification create
private static final String CREATE_TABLE_NOTIFICATION = "CREATE TABLE " + TABLE_NOTIFICATION
+ "(" + KEY_ID + " INTEGER PRIMARY KEY,"
+ MESSAGE + " TEXT NOT NULL"
+ ")";
/**
* constructor with args
*/
public DBHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_TABLE_FIXTURES);
db.execSQL(CREATE_TABLE_DIARY);
db.execSQL(CREATE_TABLE_NOTIFICATION);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_FIXTURES);
db.execSQL("DROP TABLE IF EXISTS " + TABLE_DIARY);
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NOTIFICATION);
onCreate(db);
}
public boolean insertFixture(String date, String time, String opponent, String venue){
SQLiteDatabase sqLiteDatabase = getWritableDatabase();
ContentValues values = new ContentValues();
values.put(MATCH_DATE, date);
values.put(MATCH_TIME, time);
values.put(MATCH_OPPONENT, opponent);
values.put(MATCH_VENUE, venue);
long result = sqLiteDatabase.insert(TABLE_FIXTURES, null, values);
if (result == -1) {
return false;
} else {
return true;
}
}
}
插入方法看起来像
public boolean insertFixture(String date, String time, String opponent, String venue){
SQLiteDatabase sqLiteDatabase = getWritableDatabase();
ContentValues values = new ContentValues();
values.put(MATCH_DATE, date);
values.put(MATCH_TIME, time);
values.put(MATCH_OPPONENT, opponent);
values.put(MATCH_VENUE, venue);
long result = sqLiteDatabase.insert(TABLE_FIXTURES, null, values);
if (result == -1) {
return false;
} else {
return true;
}
}
非常感谢任何帮助。
由于
答案 0 :(得分:1)
您尚未在UpdateSchedule中为DBHelper创建实例。在没有为DBHelper创建实例的情况下,您尝试调用它上面的方法。