前提・実現したいこと
ボタンを押すと入力したテキストをデータベースに保存するアプリを作成したいです。参考書に記載されているプログラムをそのまま実行しましたが、エラーが出てしまいます。
発生している問題・エラーメッセージ
E/SQLiteLog: (1) table cocktailmemos has no column named note E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.myapplication, PID: 17187 java.lang.IllegalStateException: Could not execute method for android:onClick at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:414) at android.view.View.performClick(View.java:6597) at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:992) at android.view.View.performClickInternal(View.java:6574) at android.view.View.access$3100(View.java:778) at android.view.View$PerformClick.run(View.java:25885) at android.os.Handler.handleCallback(Handler.java:873) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:193) at android.app.ActivityThread.main(ActivityThread.java:6669) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858) Caused by: java.lang.reflect.InvocationTargetException at java.lang.reflect.Method.invoke(Native Method) at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:409) at android.view.View.performClick(View.java:6597) at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:992) at android.view.View.performClickInternal(View.java:6574) at android.view.View.access$3100(View.java:778) at android.view.View$PerformClick.run(View.java:25885) at android.os.Handler.handleCallback(Handler.java:873) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:193) at android.app.ActivityThread.main(ActivityThread.java:6669) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858) Caused by: android.database.sqlite.SQLiteException: table cocktailmemos has no column named note (code 1 SQLITE_ERROR): , while compiling: INSERT INTO cocktailmemos (_id,name,note) VALUES (?,?,?) at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method) at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:903) at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:514) 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.compileStatement(SQLiteDatabase.java:1086) at com.example.myapplication.MainActivity3.onSaveButtonClick(MainActivity3.kt:44) at java.lang.reflect.Method.invoke(Native Method) at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:409) at android.view.View.performClick(View.java:6597) at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:992) at android.view.View.performClickInternal(View.java:6574) at android.view.View.access$3100(View.java:778) at android.view.View$PerformClick.run(View.java:25885) at android.os.Handler.handleCallback(Handler.java:873) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:193) at android.app.ActivityThread.main(ActivityThread.java:6669) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
該当のソースコード
MainActivity3.kt
1package com.example.myapplication 2 3import androidx.appcompat.app.AppCompatActivity 4import android.os.Bundle 5import android.view.View 6import android.widget.* 7 8class MainActivity3 : AppCompatActivity() { 9 private var _cocktailId = -1 10 11 private var _cocktailName = "" 12 13 private val _helper = DatabaseHelper(this@MainActivity3) 14 15 override fun onCreate(savedInstanceState: Bundle?) { 16 super.onCreate(savedInstanceState) 17 setContentView(R.layout.activity_main3) 18 19 val lvCocktail = findViewById<ListView>(R.id.lvCoctail) 20 lvCocktail.onItemClickListener = ListItemClickListener() 21 } 22 23 override fun onDestroy() { 24 _helper.close() 25 super.onDestroy() 26 } 27 fun onSaveButtonClick(view: View){ 28 val etNote = findViewById<EditText>(R.id.etNote) 29 30 val note = etNote.text.toString() 31 32 val db = _helper.writableDatabase 33 34 val sqlDelete = "DELETE FROM cocktailmemos WHERE _id = ?" 35 36 var stmt = db.compileStatement(sqlDelete) 37 38 stmt.bindLong(1,_cocktailId.toLong()) 39 40 stmt.executeUpdateDelete() 41 42 val sqlInsert = "INSERT INTO cocktailmemos (_id,name,note) VALUES (?,?,?)" 43 44 stmt = db.compileStatement(sqlInsert) 45 46 stmt.bindLong(1,_cocktailId.toLong()) 47 stmt.bindString(2,_cocktailName) 48 stmt.bindString(3,note) 49 50 stmt.executeInsert() 51 52 etNote.setText("") 53 54 val tvCocktailName = findViewById<TextView>(R.id.tvCocktailName) 55 56 tvCocktailName.text = getString(R.string.tv_name) 57 58 val btnSave = findViewById<Button>(R.id.btnSave) 59 60 btnSave.isEnabled = false 61 62 } 63 private inner class ListItemClickListener : AdapterView.OnItemClickListener{ 64 override fun onItemClick(parent: AdapterView<*>?, view: View?, position: Int, id: Long) { 65 _cocktailId = position 66 67 if (parent != null) { 68 _cocktailName = parent.getItemAtPosition(position) as String 69 } 70 71 72 val tvCocktailName = findViewById<TextView>(R.id.tvCocktailName) 73 74 tvCocktailName.text = _cocktailName 75 76 val btnSave = findViewById<Button>(R.id.btnSave) 77 78 btnSave.isEnabled = true 79 80 val db = _helper.writableDatabase 81 82 val sql = "SELECT * FROM cocktailmemos WHERE _id = ${_cocktailId}" 83 84 val cursor = db.rawQuery(sql,null) 85 86 var note = "" 87 88 while(cursor.moveToNext()){ 89 val idxNote = cursor.getColumnIndex("note") 90 91 note = cursor.getString(idxNote) 92 } 93 val etNote = findViewById<EditText>(R.id.etNote) 94 etNote.setText(note) 95 96 } 97 98 99 } 100}
補足情報(FW/ツールのバージョンなど)
恐らくonSaveButtonClickの部分でエラーが出ているのではないかと考えていました。
宜しくお願い致します。
あなたの回答
tips
プレビュー