应用程序没有编码错误,但它在启动时崩溃。在 LogCat 中,我看到错误没有找到列,但我不知道问题出在哪里。我做了很多研究,但到目前为止还没有成功。这是我的课程的源代码:
MainActivity 类:
public class MainActivity extends ListActivity {
private StudentOperations studentDBoperation;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
studentDBoperation = new StudentOperations(this);
studentDBoperation.open();
List values = studentDBoperation.getAllStudents();
// Use the SimpleCursorAdapter to show the
// elements in a ListView
ArrayAdapter adapter = new ArrayAdapter(this,
android.R.layout.simple_list_item_1, values);
setListAdapter(adapter);
}
public void addUser(View view) {
ArrayAdapter adapter = (ArrayAdapter) getListAdapter();
EditText text = (EditText) findViewById(R.id.editText1);
EditText text1 = (EditText) findViewById(R.id.editText2);
Student stud = studentDBoperation.addStudent(text.getText().toString(),text1.getText().toString());
adapter.add(stud);
}
public void deleteFirstUser(View view) {
ArrayAdapter adapter = (ArrayAdapter) getListAdapter();
Student stud = null;
if (getListAdapter().getCount() > 0) {
stud = (Student) getListAdapter().getItem(0);
studentDBoperation.deleteStudent(stud);
adapter.remove(stud);
}
}
@Override
protected void onResume() {
studentDBoperation.open();
super.onResume();
}
@Override
protected void onPause() {
studentDBoperation.close();
super.onPause();
}
}
学生班级:
public class Student {
private int id;
private String name;
private String password;
public long getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return name;
}
public String getPassword() {
return this.password;
}
public void setPassword(String password) {
this.password = password;
}
}
学生操作课:
public class StudentOperations {
// Database fields
private DataBaseWrapper dbHelper;
private String[] STUDENT_TABLE_COLUMNS = { DataBaseWrapper.STUDENT_ID, DataBaseWrapper.STUDENT_NAME, DataBaseWrapper.STUDENT_PASSWORD };
private SQLiteDatabase database;
public StudentOperations(Context context) {
dbHelper = new DataBaseWrapper(context);
}
public void open() throws SQLException {
database = dbHelper.getWritableDatabase();
}
public void close() {
dbHelper.close();
}
public Student addStudent(String name,String password) {
ContentValues values = new ContentValues();
values.put(DataBaseWrapper.STUDENT_NAME, name);
values.put(DataBaseWrapper.STUDENT_PASSWORD, password);
long studId = database.insert(DataBaseWrapper.STUDENTS, null, values);
// now that the student is created return it ...
Cursor cursor = database.query(DataBaseWrapper.STUDENTS,
STUDENT_TABLE_COLUMNS, DataBaseWrapper.STUDENT_ID + " = "
+ studId, null, null, null, null);
cursor.moveToFirst();
Student newComment = parseStudent(cursor);
cursor.close();
return newComment;
}
public void deleteStudent(Student comment) {
long id = comment.getId();
System.out.println("Comment deleted with id: " + id);
database.delete(DataBaseWrapper.STUDENTS, DataBaseWrapper.STUDENT_ID
+ " = " + id, null);
}
public List getAllStudents() {
List students = new ArrayList();
Cursor cursor = database.query(DataBaseWrapper.STUDENTS,
STUDENT_TABLE_COLUMNS, null, null, null, null, null);
cursor.moveToFirst();
while (!cursor.isAfterLast()) {
Student student = parseStudent(cursor);
students.add(student);
cursor.moveToNext();
}
cursor.close();
return students;
}
private Student parseStudent(Cursor cursor) {
Student student = new Student();
student.setId((cursor.getInt(0)));
student.setName(cursor.getString(1));
return student;
}
}
DataBaseWrapper 类:
public class DataBaseWrapper extends SQLiteOpenHelper {
public static final String STUDENTS = "Students";
public static final String STUDENT_ID = "_id";
public static final String STUDENT_NAME = "name";
public static final String STUDENT_PASSWORD = "password";
private static final String DATABASE_NAME = "Students.db";
private static final int DATABASE_VERSION = 1;
// creation SQLite statement
private static final String DATABASE_CREATE = "create table " + STUDENTS
+ "(" + STUDENT_ID + " integer primary key autoincrement, " + STUDENT_NAME + " text not null " + STUDENT_PASSWORD + " text not null ); ";
public DataBaseWrapper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(DATABASE_CREATE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// you should do some logging in here
// ..
db.execSQL("DROP TABLE IF EXISTS " + STUDENTS);
onCreate(db);
}
}
LogCat 错误:
11-05 21:51:02.430:D/AbsListView(26729):获取 MotionRecognitionManager 11-05 21:51:02.470:E/SQLiteLog(26729):(1) 没有这样的列:名称 11-05 21:51:02.470 :D / AndroidRuntime(26729):关闭VM 11-05 21:51:02.470:W / dalvikvm(26729):threadid = 1:线程退出但未捕获异常(组= 0x410762a0)11-05 21:51:02.480: E/AndroidRuntime(26729): 致命异常: main 11-05 21:51:02.480: E/AndroidRuntime(26729): java.lang.RuntimeException: 无法启动活动 ComponentInfo{com.example.sqlite/com.example.sqlite .MainActivity}:android.database.sqlite.SQLiteException:没有这样的列:名称(代码1):,编译时:SELECT _id,名称,密码来自学生11-05 21:51:02.480:E / AndroidRuntime(26729):在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2100) 11-05 21:51:02.480: E/AndroidRuntime(26729): 在 android.app。ActivityThread.handleLaunchActivity(ActivityThread.java:2125) 11-05 21:51:02.480: E/AndroidRuntime(26729): 在 android.app.ActivityThread.access$600(ActivityThread.java:140) 11-05 21:51:02.480 : E/AndroidRuntime(26729): 在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:1227) 11-05 21:51:02.480: E/AndroidRuntime(26729): 在 android.os.Handler.dispatchMessage( Handler.java:99) 11-05 21:51:02.480: E/AndroidRuntime(26729): 在 android.os.Looper.loop(Looper.java:137) 11-05 21:51:02.480: E/AndroidRuntime( 26729): 在 android.app.ActivityThread.main(ActivityThread.java:4898) 11-05 21:51:02.480: E/AndroidRuntime(26729): 在 java.lang.reflect.Method.invokeNative(Native Method) 11- 05 21:51:02.480: E/AndroidRuntime(26729): at java.lang.reflect.Method.invoke(Method.java:511) 11-05 21:51:02.480: E/AndroidRuntime(26729): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006) 11-05 21:51:02.480: E/AndroidRuntime(26729): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java :773) 11-05 21:51:02.480: E/AndroidRuntime(26729): at dalvik.system.NativeStart.main(Native Method) 11-05 21:51:02.480: E/AndroidRuntime(26729): 原因: android.database.sqlite.SQLiteException: no such column: name (code 1): , while compile: SELECT _id, name, password FROM Students 11-05 21:51:02.480: E/AndroidRuntime(26729): at android.database .sqlite.SQLiteConnection.nativePrepareStatement(Native Method) 11-05 21:51:02.480: E/AndroidRuntime(26729): 在 android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:1012) 11-05 21:51: 02.480:E/AndroidRuntime(26729):在 android.database.sqlite.SQLiteConnection。准备(SQLiteConnection.java:623)11-05 21:51:02.480:E/AndroidRuntime(26729):在 android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)11-05 21:51:02.480: E/AndroidRuntime(26729): 在 android.database.sqlite.SQLiteProgram.(SQLiteProgram.java:58) 11-05 21:51:02.480: E/AndroidRuntime(26729): 在 android.database.sqlite.SQLiteQuery.(SQLiteQuery .java:37) 11-05 21:51:02.480: E/AndroidRuntime(26729): 在 android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44) 11-05 21:51:02.480: E/AndroidRuntime (26729): 在 android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1314) 11-05 21:51:02.480: E/AndroidRuntime(26729): 在 android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase. java:1161) 11-05 21:51:02.480: E/AndroidRuntime(26729): 在 android.database。sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1032) 11-05 21:51:02.480: E/AndroidRuntime(26729): 在 android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1200) 11-05 21: 51:02.480: E/AndroidRuntime(26729): 在 com.example.sqlite.StudentOperations.getAllStudents(StudentOperations.java:62) 11-05 21:51:02.480: E/AndroidRuntime(26729): 在 com.example.sqlite .MainActivity.onCreate(MainActivity.java:23) 11-05 21:51:02.480: E/AndroidRuntime(26729): 在 android.app.Activity.performCreate(Activity.java:5206) 11-05 21:51:02.480 : E/AndroidRuntime(26729): 在 android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1083) 11-05 21:51:02.480: E/AndroidRuntime(26729): 在 android.app.ActivityThread.performLaunchActivity(ActivityThread. java:2064) 11-05 21:51:02.480: E/AndroidRuntime(26729): ... 11 更多查询(SQLiteDatabase.java:1032)11-05 21:51:02.480:E/AndroidRuntime(26729):在 android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1200)11-05 21:51:02.480: E/AndroidRuntime(26729): 在 com.example.sqlite.StudentOperations.getAllStudents(StudentOperations.java:62) 11-05 21:51:02.480: E/AndroidRuntime(26729): 在 com.example.sqlite.MainActivity.onCreate (MainActivity.java:23) 11-05 21:51:02.480: E/AndroidRuntime(26729): 在 android.app.Activity.performCreate(Activity.java:5206) 11-05 21:51:02.480: E/AndroidRuntime (26729): 在 android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1083) 11-05 21:51:02.480: E/AndroidRuntime(26729): 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2064) 11-05 21:51:02.480: E/AndroidRuntime(26729): ... 11 更多查询(SQLiteDatabase.java:1032)11-05 21:51:02.480:E/AndroidRuntime(26729):在 android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1200)11-05 21:51:02.480: E/AndroidRuntime(26729): 在 com.example.sqlite.StudentOperations.getAllStudents(StudentOperations.java:62) 11-05 21:51:02.480: E/AndroidRuntime(26729): 在 com.example.sqlite.MainActivity.onCreate (MainActivity.java:23) 11-05 21:51:02.480: E/AndroidRuntime(26729): 在 android.app.Activity.performCreate(Activity.java:5206) 11-05 21:51:02.480: E/AndroidRuntime (26729): 在 android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1083) 11-05 21:51:02.480: E/AndroidRuntime(26729): 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2064) 11-05 21:51:02.480: E/AndroidRuntime(26729): ... 11 更多480: E/AndroidRuntime(26729): 在 android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1200) 11-05 21:51:02.480: E/AndroidRuntime(26729): 在 com.example.sqlite.StudentOperations .getAllStudents(StudentOperations.java:62) 11-05 21:51:02.480: E/AndroidRuntime(26729): 在 com.example.sqlite.MainActivity.onCreate(MainActivity.java:23) 11-05 21:51:02.480 : E/AndroidRuntime(26729): 在 android.app.Activity.performCreate(Activity.java:5206) 11-05 21:51:02.480: E/AndroidRuntime(26729): 在 android.app.Instrumentation.callActivityOnCreate(Instrumentation. java:1083) 11-05 21:51:02.480: E/AndroidRuntime(26729): 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2064) 11-05 21:51:02.480: E/AndroidRuntime(26729) : ... 11 更多480: E/AndroidRuntime(26729): 在 android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1200) 11-05 21:51:02.480: E/AndroidRuntime(26729): 在 com.example.sqlite.StudentOperations .getAllStudents(StudentOperations.java:62) 11-05 21:51:02.480: E/AndroidRuntime(26729): 在 com.example.sqlite.MainActivity.onCreate(MainActivity.java:23) 11-05 21:51:02.480 : E/AndroidRuntime(26729): 在 android.app.Activity.performCreate(Activity.java:5206) 11-05 21:51:02.480: E/AndroidRuntime(26729): 在 android.app.Instrumentation.callActivityOnCreate(Instrumentation. java:1083) 11-05 21:51:02.480: E/AndroidRuntime(26729): 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2064) 11-05 21:51:02.480: E/AndroidRuntime(26729) : ... 11 更多查询(SQLiteDatabase.java:1200)11-05 21:51:02.480:E/AndroidRuntime(26729):在 com.example.sqlite.StudentOperations.getAllStudents(StudentOperations.java:62)11-05 21:51:02.480: E/AndroidRuntime(26729): 在 com.example.sqlite.MainActivity.onCreate(MainActivity.java:23) 11-05 21:51:02.480: E/AndroidRuntime(26729): 在 android.app.Activity.performCreate(Activity .java:5206) 11-05 21:51:02.480: E/AndroidRuntime(26729): 在 android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1083) 11-05 21:51:02.480: E/AndroidRuntime(26729 ): 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2064) 11-05 21:51:02.480: E/AndroidRuntime(26729): ... 11 更多查询(SQLiteDatabase.java:1200)11-05 21:51:02.480:E/AndroidRuntime(26729):在 com.example.sqlite.StudentOperations.getAllStudents(StudentOperations.java:62)11-05 21:51:02.480: E/AndroidRuntime(26729): 在 com.example.sqlite.MainActivity.onCreate(MainActivity.java:23) 11-05 21:51:02.480: E/AndroidRuntime(26729): 在 android.app.Activity.performCreate(Activity .java:5206) 11-05 21:51:02.480: E/AndroidRuntime(26729): 在 android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1083) 11-05 21:51:02.480: E/AndroidRuntime(26729 ): 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2064) 11-05 21:51:02.480: E/AndroidRuntime(26729): ... 11 更多E/AndroidRuntime(26729): 在 com.example.sqlite.MainActivity.onCreate(MainActivity.java:23) 11-05 21:51:02.480: E/AndroidRuntime(26729): 在 android.app.Activity.performCreate(Activity .java:5206) 11-05 21:51:02.480: E/AndroidRuntime(26729): 在 android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1083) 11-05 21:51:02.480: E/AndroidRuntime(26729 ): 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2064) 11-05 21:51:02.480: E/AndroidRuntime(26729): ... 11 更多E/AndroidRuntime(26729): 在 com.example.sqlite.MainActivity.onCreate(MainActivity.java:23) 11-05 21:51:02.480: E/AndroidRuntime(26729): 在 android.app.Activity.performCreate(Activity .java:5206) 11-05 21:51:02.480: E/AndroidRuntime(26729): 在 android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1083) 11-05 21:51:02.480: E/AndroidRuntime(26729 ): 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2064) 11-05 21:51:02.480: E/AndroidRuntime(26729): ... 11 更多E/AndroidRuntime(26729): 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2064) 11-05 21:51:02.480: E/AndroidRuntime(26729): ... 11 更多E/AndroidRuntime(26729): 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2064) 11-05 21:51:02.480: E/AndroidRuntime(26729): ... 11 更多