ViewModelのことで質問させてください。該当箇所のソースを念のため載せさせて頂きます。長文で申し訳ありません
RequestDocumentsForm1Activity画面で次へボタンを押下すると、「Cannot create an instance of class」のエラーが出て画面が落ちてしまいます。
エラー全文は以下になります。
java.lang.RuntimeException: Cannot create an instance of class com.example.rworksample00025.RequestDocumentsViewModel at androidx.lifecycle.ViewModelProvider$AndroidViewModelFactory.create(ViewModelProvider.java:275) at androidx.lifecycle.SavedStateViewModelFactory.create(SavedStateViewModelFactory.java:112) at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.java:185) at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.java:150) at androidx.lifecycle.ViewModelLazy.getValue(ViewModelProvider.kt:54) at androidx.lifecycle.ViewModelLazy.getValue(ViewModelProvider.kt:41) at com.example.rworksample00025.RequestDocumentsForm1Activity$onCreate$1.onClick(RequestDocumentsForm1Activity.kt:34) at android.view.View.performClick(View.java:7448) at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:1119) at android.view.View.performClickInternal(View.java:7425) at android.view.View.access$3600(View.java:810) at android.view.View$PerformClick.run(View.java:28305) at android.os.Handler.handleCallback(Handler.java:938) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:223) at android.app.ActivityThread.main(ActivityThread.java:7660) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947) Caused by: java.lang.reflect.InvocationTargetException at java.lang.reflect.Constructor.newInstance0(Native Method) at java.lang.reflect.Constructor.newInstance(Constructor.java:343) at androidx.lifecycle.ViewModelProvider$AndroidViewModelFactory.create(ViewModelProvider.java:267) at androidx.lifecycle.SavedStateViewModelFactory.create(SavedStateViewModelFactory.java:112) at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.java:185) at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.java:150) at androidx.lifecycle.ViewModelLazy.getValue(ViewModelProvider.kt:54) at androidx.lifecycle.ViewModelLazy.getValue(ViewModelProvider.kt:41) at com.example.rworksample00025.RequestDocumentsForm1Activity$onCreate$1.onClick(RequestDocumentsForm1Activity.kt:34) at android.view.View.performClick(View.java:7448) at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:1119) at android.view.View.performClickInternal(View.java:7425) at android.view.View.access$3600(View.java:810) at android.view.View$PerformClick.run(View.java:28305) at android.os.Handler.handleCallback(Handler.java:938) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:223) at android.app.ActivityThread.main(ActivityThread.java:7660) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947) Caused by: java.lang.IllegalStateException: Cannot access database on the main thread since it may potentially lock the UI for a long period of time. at androidx.room.RoomDatabase.assertNotMainThread(RoomDatabase.java:267) at androidx.room.RoomDatabase.query(RoomDatabase.java:323) at androidx.room.util.DBUtil.query(DBUtil.java:83) at com.example.rworksample00025.RequestDocumentsPersonInfoDao_Impl.loadAllRequestDocumentsPersonInfo(RequestDocumentsPersonInfoDao_Impl.java:229) at com.example.rworksample00025.RequestDocumentsViewModel.<init>(RequestDocumentsViewModel.kt:19) at java.lang.reflect.Constructor.newInstance0(Native Method) at java.lang.reflect.Constructor.newInstance(Constructor.java:343) at androidx.lifecycle.ViewModelProvider$AndroidViewModelFactory.create(ViewModelProvider.java:267) at androidx.lifecycle.SavedStateViewModelFactory.create(SavedStateViewModelFactory.java:112) at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.java:185) at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.java:150) at androidx.lifecycle.ViewModelLazy.getValue(ViewModelProvider.kt:54) at androidx.lifecycle.ViewModelLazy.getValue(ViewModelProvider.kt:41) at com.example.rworksample00025.RequestDocumentsForm1Activity$onCreate$1.onClick(RequestDocumentsForm1Activity.kt:34) at android.view.View.performClick(View.java:7448) at (略)
全体のソースコード
ViewModelが以下です。
kotlin
1package com.example.rworksample00025 2 3import android.app.Application 4import androidx.lifecycle.AndroidViewModel 5import androidx.lifecycle.ViewModel 6import androidx.lifecycle.viewModelScope 7import kotlinx.coroutines.Dispatchers 8import kotlinx.coroutines.launch 9 10class RequestDocumentsViewModel(application: Application): AndroidViewModel(application) { 11 12 private val requestDocumentsPersonInfoDao: RequestDocumentsPersonInfoDao 13 14 init { 15 val db = RequestDocumentsPersonInfoDatabase.buildDatabase(application) 16 requestDocumentsPersonInfoDao = db.requestDocumentsPersonInfoDao() 17 } 18 19 val personData = requestDocumentsPersonInfoDao.loadAllRequestDocumentsPersonInfo() 20 val personDataAll = requestDocumentsPersonInfoDao.loadAllRequestDocumentsPersonInfoLiveData() 21 22 fun insert(name: String, phoneticGuides: String, birthday: String, mailAddress: String, phoneNumber: String) { 23 viewModelScope.launch(Dispatchers.IO) { 24 requestDocumentsPersonInfoDao.saveRequestDocumentsPersonInfo( 25 RequestDocumentsPersonInfo( 26 id = 0, 27 name = name, 28 phoneticGuides = phoneticGuides, 29 birthday = birthday, 30 mailAddress = mailAddress, 31 phoneNumber = phoneNumber, 32 zipStr = "", 33 prefecture = "", 34 city = "", 35 address = "", 36 disableCertifidate = "", 37 sendingMaterials = "", 38 remarks = "" 39 ) 40 ) 41 } 42 } 43 44 (略) 45}
ViewModelを呼び出している該当の箇所のActivityです。
kotlin
1package com.example.rworksample00025 2 3import android.app.DatePickerDialog 4import android.content.Intent 5import androidx.appcompat.app.AppCompatActivity 6import android.os.Bundle 7import android.util.Log 8import android.util.Patterns 9import android.view.View 10import android.widget.DatePicker 11import android.widget.EditText 12import android.widget.Toast 13import androidx.activity.viewModels 14import com.example.rworksample00025.databinding.ActivityRequestDocumentsForm1Binding 15import java.util.* 16 17class RequestDocumentsForm1Activity : AppCompatActivity(), DatePickerDialog.OnDateSetListener { 18 19 private lateinit var binding: ActivityRequestDocumentsForm1Binding 20 21 22 override fun onCreate(savedInstanceState: Bundle?) { 23 super.onCreate(savedInstanceState) 24 binding = ActivityRequestDocumentsForm1Binding.inflate(layoutInflater) 25 setContentView(binding.root) 26 27 val viewModel: RequestDocumentsViewModel by viewModels() 28 29 binding.nextPageBtn.setOnClickListener (object: View.OnClickListener { 30 override fun onClick(v: View?) { 31 val check = validationCheck(binding.name, binding.mailAddress, binding.phoneNumber) 32 if (check) { 33 viewModel.insert(binding.name.text.toString(), binding.phoneticGuides.text.toString(), binding.birthday.text.toString(), binding.mailAddress.text.toString(), binding.phoneNumber.text.toString()) 34 val intent = Intent(this@RequestDocumentsForm1Activity, RequestDocumentsForm2Activity::class.java) 35 startActivity(intent) 36 } 37 } 38 }) 39 40 41 } 42 43 fun validationCheck(name: EditText, mail: EditText, phone: EditText) : Boolean { 44 45 if (binding.name.text.toString().length == 0) { 46 47 binding.name.requestFocus() 48 49 Toast.makeText(applicationContext, "名前が未入力です。", Toast.LENGTH_SHORT).show() 50 return false 51 } 52 53 if (binding.mailAddress.text.toString().length == 0 && !Patterns.EMAIL_ADDRESS.matcher(mail.text.toString()).matches()){ 54 binding.mailAddress.requestFocus() 55 Toast.makeText(applicationContext, "メールアドレスが未入力または不正です。", Toast.LENGTH_SHORT).show() 56 return false 57 } 58 59 if (binding.phoneNumber.text.toString().length == 0) { 60 binding.phoneNumber.requestFocus() 61 Toast.makeText(applicationContext, "電話番号が未入力です。", Toast.LENGTH_SHORT).show() 62 return false 63 } 64 return true 65 } 66 (略)
環境情報
OS: Windows10
Android Studio 4.1.2
Kotlin 1.4.30
使用したライブラリ:
SQLCipher
回答1件
あなたの回答
tips
プレビュー