应用程序没有编码错误,但它在启动时崩溃。在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;
}
}
StudentOperation课程:
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):致命异常:主要 11-05 21:51:02.480:E / AndroidRuntime(26729):java.lang.RuntimeException:无法启动活动ComponentInfo {com.example.sqlite / com.example.sqlite.MainActivity}:android.database.sqlite.SQLiteException :没有这样的列:name(代码1):,同时编译:SELECT _id,name,password FROM Students 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):at 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:没有这样的列:name(代码1):,同时编译:SELECT _id,name,password FROM Students 11-05 21:51:02.480:E / AndroidRuntime(26729):在android.database.sqlite.SQLiteConnection.nativePrepareStatement(本机方法) 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.prepare(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):at com.example.sqlite.StudentOperations.getAllStudents(StudentOperations.java:62) 11-05 21:51:02.480:E / AndroidRuntime(26729):at 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更多
答案 0 :(得分:1)
在DATABASE_CREATE语句中,缺少名称和密码列定义之间的逗号。
答案 1 :(得分:1)
当我在我的Databasehandler文件(在你的情况下为DataBaseWrapper)中更改(即附加了一个附加列)时,我遇到了类似的错误。从设备上卸下我的应用程序然后再次安装后,我发现它已解决。看到它。
答案 2 :(得分:0)
您需要在create语句中添加逗号并从中删除分号。尝试更改:
// 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 ); ";
为:
// 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 )";