アプリにはコーディング エラーはありませんが、起動時にクラッシュします。私は多くの研究を行ってきましたが、これまでのところ成功していません。私のクラスのソースコードは次のとおりです。
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 を取得します: D/AndroidRuntime(26729): VM のシャットダウン 11-05 21:51:02.470: W/dalvikvm(26729): threadid=1: キャッチされない例外で終了するスレッド (group=0x410762a0) 11-05 21:51:02.480: E/AndroidRuntime(26729): FATAL EXCEPTION: main 11-05 21:51:02.480: E/AndroidRuntime(26729): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.sqlite/com.example.sqlite .MainActivity}: android.database.sqlite.SQLiteException: そのような列はありません: 名前 (コード 1): 、コンパイル中: SELECT _id、名前、パスワード FROM 学生 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(ネイティブ メソッド) 11- 05 21:51:02.480: E/AndroidRuntime(26729): java.lang.reflect.Method.invoke(Method.java:511) 11-05 21:51:02.480: E/AndroidRuntime(26729): com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006) 11-05 21:51:02.480: E/AndroidRuntime(26729): com.android.internal.os.ZygoteInit.main(ZygoteInitjava.java) :773) 11-05 21:51:02.480: E/AndroidRuntime(26729): dalvik.system.NativeStart.main(ネイティブ メソッド) 11-05 21:51:02.480: E/AndroidRuntime(26729): 原因: android.database.sqlite.SQLiteException: そのような列はありません: 名前 (コード 1): 、コンパイル中: SELECT _id、名前、パスワード FROM 学生 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 で。準備 (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) ): at 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) ): at 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) ): at 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) ): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2064) 11-05 21:51:02.480: E/AndroidRuntime(26729): ... 11 もっと見るE/AndroidRuntime(26729): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2064) 11-05 21:51:02.480: E/AndroidRuntime(26729): ... 11 もっと見るE/AndroidRuntime(26729): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2064) 11-05 21:51:02.480: E/AndroidRuntime(26729): ... 11 もっと見る