构建一个TODO列表应用程序,用于在SQL数据库中保存用户Edittext数据,但是当我单击该按钮时它会崩溃但重新启动而不保存用户输入错误
Logcat错误
<er-response id="100053">
<payload>
<modify-usergroup-reponse>
<success>true</success>
<reason-code>
<code>0</code>
<sub-code>0</sub-code>
<name>OK</name>
</reason-code>
</modify-usergroup-reponse>
</payload>
</er-response>
点击方法
0
8-06 23:36:23.018 2904-2904/com.naive.LISTY E/SQLiteLog: (1) table tasks has no column named date
08-06 23:36:23.019 2904-2904/com.naive.LISTY E/SQLiteDatabase: Error inserting date=12/12/12 Description=hello world title=Hello time=12:50
android.database.sqlite.SQLiteException: table tasks has no column named date (code 1): , while compiling: INSERT INTO tasks(date,Description,title,time) VALUES (?,?,?,?)
at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:887)
at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:498)
at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1469)
at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1341)
at listy.naive.com.listy.Dialog.Add_task(Dialog.java:63)
at java.lang.reflect.Method.invoke(Native Method)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:284)
at android.view.View.performClick(View.java:5198)
at android.view.View$PerformClick.run(View.java:21147)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
光标代码
public void Add_task(View view) {
EditText mTitle = (EditText)findViewById(R.id.title_field);
String Title = String.valueOf(mTitle.getText());
EditText mTask = (EditText)findViewById(R.id.description_field);
String Task = String.valueOf(mTask.getText());
EditText mDate = (EditText) findViewById(R.id.date_field);
String Date = String.valueOf(mDate.getText());
EditText mTime = (EditText) findViewById(R.id.time_field);
String Time = String.valueOf(mTime.getText());
SQLiteDatabase db = mHelper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(TaskContract.TaskEntry.COL_TASK_TITLE, Title);
values.put(TaskContract.TaskEntry.COL_TASK_DESCRIPTION, Task);
values.put(TaskContract.TaskEntry.COL_TASK_DATE, Date);
values.put(TaskContract.TaskEntry.COL_TASK_TIME, Time);
db.insert(TaskContract.TaskEntry.TABLE, null, values);
//updateUI();
//db.close();
}
mHelper
private Cursor updateUI() {
ArrayList<String> taskList = new ArrayList<>();
SQLiteDatabase db = mHelper.getReadableDatabase();
Cursor cursor = db.query(TaskContract.TaskEntry.TABLE,
new String[]{TaskContract.TaskEntry._ID, TaskContract.TaskEntry.COL_TASK_TITLE, TaskContract.TaskEntry.COL_TASK_DESCRIPTION, TaskContract.TaskEntry.COL_TASK_DATE, TaskContract.TaskEntry.COL_TASK_TIME},
null, null, null, null, null);
return cursor;}
答案 0 :(得分:0)
我认为mHelper
是null
。我认为你没有初始化mHelper
。
答案 1 :(得分:0)
此行导致NullPointerException
:
SQLiteDatabase db = mHelper.getWritableDatabase();
您尚未在任何地方初始化mHelper
,因此您获得了例外。你需要添加:
MySQLiteOpenHelper mHelper = new MySQLiteOpenHelper(parameters);
MySQLiteOpenHelper
是你的一类,它扩展了SQLiteOpenHelper
并覆盖了你需要的一些方法。
修改强>
在使用SQLiteOpenHelper
的实施编辑问题后,请执行以下操作:
TaskDbHelper mHelper = new TaskDbHelper(this);
如果您在活动中传递this
,并且在片段中,请将getActivity()
作为参数传递。