在SQLite服务器Android Studio中输入时间

时间:2017-10-06 01:04:03

标签: java android sqlite

所以我试图使用timepickerdialog来接收用户输入的时间并将其放入android studio上的sqlite服务器中。一旦用户输入时间,我就遇到了麻烦。

TimePickerFragment.java

public class TimePickerFragment extends DialogFragment implements TimePickerDialog.OnTimeSetListener{

private HabitHelper mHelper;

@RequiresApi(api = Build.VERSION_CODES.N)
@Override
public Dialog onCreateDialog(Bundle savedInstanceState){
    final Calendar c = Calendar.getInstance();
    int hour = c.get(Calendar.HOUR_OF_DAY);
    int minute = c.get(Calendar.MINUTE);

    return new TimePickerDialog(getActivity(), this, hour, minute, DateFormat.is24HourFormat(getActivity()));

}


@Override
public void onTimeSet(TimePicker timePicker, int hourOfDay, int minute) {
    SQLiteDatabase db = mHelper.getWritableDatabase();

    ContentValues values = new ContentValues();

    String time = String.valueOf(hourOfDay) + ":" + String.valueOf(minute);

    values.put(HabitContract.HabitEntry.COLUMN_TIME, time);

}

HabitHelper.java

public class HabitHelper extends SQLiteOpenHelper {

public static final String DB_NAME = "HabitHamster";
public static final int DB_Version = 3;

public HabitHelper(Context context){
    super(context, DB_NAME, null, DB_Version);
}


@Override
public void onCreate(SQLiteDatabase db) {
    String createTable = "CREATE TABLE "
            + HabitContract.HabitEntry.TABLE + " ( "
            + HabitContract.HabitEntry._ID
            + " INTEGER PRIMARY KEY, "
            + HabitContract.HabitEntry.COLUMN_NAME_TITLE
            + " TEXT NOT NULL, "
            + HabitContract.HabitEntry.COLUMN_TIME
            + " TIMESTAMP DEFAULT CURRENT_TIMESTAMP"
            + ");";

    db.execSQL(createTable);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL("DROP TABLE IF EXISTS " + HabitContract.HabitEntry.TABLE);
    onCreate(db);
}

HabitContract.java

public class HabitContract {

private HabitContract() {}

public class HabitEntry implements BaseColumns{
    public static final String TABLE = "habits";
    public static final String COLUMN_NAME_TITLE = "title";
    public static final String COLUMN_TIME = "time";
}

继承我正在使用timepickerdialog

的MainActivity部分
 mHabitView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {

        @Override
        public boolean onItemLongClick(AdapterView<?> adapterView, View view, int position, long id) {
            View v = mHabitView.getChildAt(position);
            CheckedTextView ctv = v.findViewById(R.id.habitItem);
            DialogFragment newFragment = new TimePickerFragment();
            newFragment.show(getFragmentManager(), "TimePicker");
            return true;
        }
    });

编辑:对不起,我在用户输入时间后收到的错误是:

10-05 18:05:37.281 32708-32708/com.example.jonathan.myapplication E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                Process: com.example.jonathan.myapplication, PID: 32708
                                                                                java.lang.NullPointerException: Attempt to invoke virtual method 'android.database.sqlite.SQLiteDatabase com.example.jonathan.myapplication.db.HabitHelper.getWritableDatabase()' on a null object reference
                                                                                    at com.example.jonathan.myapplication.TimePickerFragment.onTimeSet(TimePickerFragment.java:45)
                                                                                    at android.app.TimePickerDialog.onClick(TimePickerDialog.java:169)
                                                                                    at android.app.TimePickerDialog$1.onClick(TimePickerDialog.java:154)
                                                                                    at android.view.View.performClick(View.java:6256)
                                                                                    at android.view.View$PerformClick.run(View.java:24697)
                                                                                    at android.os.Handler.handleCallback(Handler.java:789)
                                                                                    at android.os.Handler.dispatchMessage(Handler.java:98)
                                                                                    at android.os.Looper.loop(Looper.java:164)
                                                                                    at android.app.ActivityThread.main(ActivityThread.java:6541)
                                                                                    at java.lang.reflect.Method.invoke(Native Method)
                                                                                    at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
                                                                                    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)

0 个答案:

没有答案