我是一個正在開發將顯示待辦事項列表的應用程序項目的新手/學生。我一直在使用警報對話框來獲取用戶的輸入並將後者存儲在一個表中。當我擴大到2個對話框,以獲取有關任務和主題兩個信息,出現下列錯誤:Android Studio中沒有這樣的列錯誤
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.zilunlin.bacpack, PID: 8118
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.zilunlin.bacpack/com.example.zilunlin.bacpack.MainActivity}: android.database.sqlite.SQLiteException: no such column: subject (code 1): , while compiling: SELECT _id, title, subject FROM tasks
#################################################################
Error Code : 1 (SQLITE_ERROR)
Caused By : SQL(query) error or missing database.
(no such column: subject (code 1): , while compiling: SELECT _id, title, subject FROM tasks)
#################################################################
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3254)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3350)
at android.app.ActivityThread.access$1100(ActivityThread.java:222)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1795)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:7229)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
Caused by: android.database.sqlite.SQLiteException: no such column: subject (code 1): , while compiling: SELECT _id, title, subject FROM tasks
#################################################################
Error Code : 1 (SQLITE_ERROR)
Caused By : SQL(query) error or missing database.
(no such column: subject (code 1): , while compiling: SELECT _id, title, subject FROM tasks)
#################################################################
at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:1058)
at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:623)
at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:59)
at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:37)
at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44)
at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1454)
at android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1301)
at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1172)
at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1340)
at com.example.zilunlin.bacpack.MainActivity.updateUI(MainActivity.java:128)
at com.example.zilunlin.bacpack.MainActivity.onCreate(MainActivity.java:36)
at android.app.Activity.performCreate(Activity.java:6876)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1135)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3207)
... 9 more
注意,不直接檢測到這個錯誤,只是表面的,當我運行調試。
以下是MainActivity我的代碼,特別是對話
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_add_task:
final EditText taskEditText = new EditText(this);
AlertDialog dialog = new AlertDialog.Builder(this)
.setTitle("Add a new task")
.setMessage("What is it that you gotta do?")
.setView(taskEditText)
.setPositiveButton("Add", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
String task = String.valueOf(taskEditText.getText());
SQLiteDatabase db = mHelper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(TaskContract.TaskEntry.COL_TASK_TITLE, task);
db.insertWithOnConflict(TaskContract.TaskEntry.TABLE,
null,
values,
SQLiteDatabase.CONFLICT_REPLACE);
db.close();
updateUI();
}
})
.setNegativeButton("Cancel", null)
.create();
dialog.show();
final EditText SubjectText = new EditText(this);
AlertDialog setSubject = new AlertDialog.Builder(this)
.setTitle("Add its subject")
.setMessage("What is its subject?")
.setView(SubjectText)
.setPositiveButton("Add", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface setSubject, int which) {
String subject = String.valueOf(SubjectText.getText());
SQLiteDatabase db = mHelper.getWritableDatabase();
ContentValues subjectvalues = new ContentValues();
subjectvalues.put(TaskContract.TaskEntry.SUBJECT_NAME, subject);
db.insertWithOnConflict(TaskContract.TaskEntry.TABLE,
null,
subjectvalues,
SQLiteDatabase.CONFLICT_REPLACE);
db.close();
updateUI();
}
})
.setNegativeButton("Cancel", null)
.create();
setSubject.show();
return true;
default:
return super.onOptionsItemSelected(item);
}}
這裏是DB合同
import android.provider.BaseColumns;
public class TaskContract {
public static final String DB_NAME = "com.zilunlin.todolist.db";
public static final int DB_VERSION = 1;
public class TaskEntry implements BaseColumns {
public static final String TABLE = "tasks";
public static final String COL_TASK_TITLE = "title";
public static final String SUBJECT_NAME = "subject";
}
}
最後,這裏是DB幫手
public class TaskDbHelper extends SQLiteOpenHelper {
public TaskDbHelper(Context context) {
super(context, TaskContract.DB_NAME, null, TaskContract.DB_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
String createTable = "CREATE TABLE " + TaskContract.TaskEntry.TABLE + " (" +
TaskContract.TaskEntry._ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
TaskContract.TaskEntry.COL_TASK_TITLE + " TEXT NOT NULL);" +
TaskContract.TaskEntry.SUBJECT_NAME + " TEXT NOT NULL);";
db.execSQL(createTable);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TaskContract.TaskEntry.TABLE);
onCreate(db);
}
}
我明白,這個問題可能會重複,但根據我目前對編程的理解,我我無法充分轉移答案。任何迴應將不勝感激。 PS:我跟隨Aldo Ziflaj的待辦事項清單上的幫助教程。
您好,非常感謝您的回覆。我嘗試了你的建議,但問題仍然存在。 – Ziiil
你得到同樣的錯誤? – 2016-11-04 10:24:45
是的,仍然是相同的錯誤。你嘗試過 – Ziiil