0

So, I am storing two textviews on the click of an imageview from the Fragment QuestionFragment.kt and sending it over another fragment (ToLearnFragment.kt) using safeargs. So when I click on the button that will take to me this Fragment, I get an error of couldn't find the implementation of Room Database. Below is the code that i have written.

MyApp.kt

@HiltAndroidApp
class MyApp:Application() {

    // No need to cancel this scope as it'll be torn down with the process
    val applicationScope = CoroutineScope(SupervisorJob())


    // Using by lazy so the database and the repository are only created when they're needed
    // rather than when the application starts
    val database by lazy { FlashCardPairDatabase.getDatabase(this,applicationScope) }
    val repository by lazy { FlashcardRepository(database.flashCardPairDao()) }

}

ToLearnFragment.kt

class ToLearnFragment : Fragment() {

    private lateinit var binding: FragmentToLearnBinding
    private lateinit var flashcardPairViewmodel: FlashcardPairViewmodel


    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        // Inflate the layout for this fragment
       binding = FragmentToLearnBinding.inflate(inflater,container,false)
        val adapter = FlashcardListAdapter()
        binding.recyclerView.adapter = adapter
        binding.recyclerView.layoutManager = LinearLayoutManager(context)
        flashcardPairViewmodel = ViewModelProvider(this,FlashcardViewModelFactory((requireActivity().application as MyApp).repository)).get(FlashcardPairViewmodel::class.java)

        flashcardPairViewmodel.allflashcardPairs.observe(viewLifecycleOwner, Observer { flashCardPair ->
            flashCardPair?.let {
                adapter.submitList(it)
            }

        })

        // Get the arguments passed from the previous fragment
        val args = arguments

        // Get the values from the arguments
        val key = args?.getString("key")
        val value = args?.getString("value")

        // Use the key and value as needed
        Log.d("ToLearnFragment", "Key: $key, Value: $value")

        return binding.root
    }


}

The Room database that i have created is below FlashCardPair.kt

@Entity("flash_pair")
data class FlashCardPair(
    @PrimaryKey @ColumnInfo(name = "front") val front: String,
    @ColumnInfo(name = "back") val back: String
)

FlashCardpairDao.kt

@Dao
interface FlashCardPairDao {

    @Query("SELECT * FROM flash_pair ORDER BY FlashCardPair ASC")
    fun getFlashcardPairs(): Flow<List<FlashCardPair>>

    @Insert(onConflict = OnConflictStrategy.IGNORE)
    fun insertFlashCardPair(flashCardPair: FlashCardPair)

    @Query("DELETE FROM flash_pair")
    fun deleteAll()

}

FlashCardPairDatabase.kt

@Database(entities = arrayOf(FlashCardPair::class), version = 1, exportSchema = false)
abstract class FlashCardPairDatabase :RoomDatabase(){

    abstract fun flashCardPairDao():FlashCardPairDao

    private class FlashcardCallback(
        private val scope: CoroutineScope
    ):Callback(){

        override fun onCreate(db: SupportSQLiteDatabase) {
            super.onCreate(db)
            INSTANCE?.let {
                scope.launch {
                    val flashcardao = it.flashCardPairDao()


                    var flashCardPair = FlashCardPair("hello","Back")
                    flashcardao.insertFlashCardPair(flashCardPair)


                }
            }

        }
    }
    companion object {
        // Singleton prevents multiple instances of database opening at the
        // same time.
        @Volatile
        private var INSTANCE: FlashCardPairDatabase? = null

        fun getDatabase(context: Context,
                        scope: CoroutineScope
        ):FlashCardPairDatabase {
            // if the INSTANCE is not null, then return it,
            // if it is, then create the database
            return INSTANCE ?: synchronized(this) {
                val instance = Room.databaseBuilder(
                    context.applicationContext,
                    FlashCardPairDatabase::class.java,
                    "flashpair_database",
                ).build()
                INSTANCE = instance
                // return instance
                instance
            }
        }
    }


}

The code that helps me to save the data is below ( It's in QuestionsFragment.kt)

// onclick listener on the image
binding.imageFlashCard.setOnClickListener {
    
    // ViewModel for counting 
    counterviewModel.addWordCount()
    binding.textCounter.text = counterviewModel.count.toString()
    // increment currentPairIndex and get the next pair
    currentPairIndex++
    if (currentPairIndex >= hashMap.size) {
        // if we have reached the end of the hashmap, start again from the beginning
        currentPairIndex = 0
    }
    currentPair = hashMap.entries.elementAt(currentPairIndex)

    // update the UI with the new pair
    binding.textCardFront.text = currentPair.key
    binding.textCardBack.text = hashMap[currentPair.key]

    //passing key and value pair to TolearnFragment.kt 
    val bundle = Bundle()
    bundle.putString("key",currentPair.key)
    bundle.putString("value",hashMap[currentPair.key])



}

Below is my logcat which shows the error of what's happening.

 FATAL EXCEPTION: main
                                                                                                    Process: com.example.visuallithuanian, PID: 3420
                                                                                                    java.lang.RuntimeException: Cannot find implementation for com.example.visuallithuanian.database.FlashCardPairDatabase. FlashCardPairDatabase_Impl does not exist
                                                                                                        at androidx.room.Room.getGeneratedImplementation(Room.kt:58)
                                                                                                        at androidx.room.RoomDatabase$Builder.build(RoomDatabase.kt:1347)
                                                                                                        at com.example.visuallithuanian.database.FlashCardPairDatabase$Companion.getDatabase(FlashCardPairDatabase.kt:53)
                                                                                                        at com.example.visuallithuanian.MyApp$database$2.invoke(MyApp.kt:19)
                                                                                                        at com.example.visuallithuanian.MyApp$database$2.invoke(MyApp.kt:19)
                                                                                                        at kotlin.SynchronizedLazyImpl.getValue(LazyJVM.kt:74)
                                                                                                        at com.example.visuallithuanian.MyApp.getDatabase(MyApp.kt:19)
                                                                                                        at com.example.visuallithuanian.MyApp$repository$2.invoke(MyApp.kt:20)
                                                                                                        at com.example.visuallithuanian.MyApp$repository$2.invoke(MyApp.kt:20)
                                                                                                        at kotlin.SynchronizedLazyImpl.getValue(LazyJVM.kt:74)
                                                                                                        at com.example.visuallithuanian.MyApp.getRepository(MyApp.kt:20)
                                                                                                        at com.example.visuallithuanian.ToLearnFragment.onCreateView(ToLearnFragment.kt:34)
                                                                                                        at androidx.fragment.app.Fragment.performCreateView(Fragment.java:3104)
                                                                                                        at androidx.fragment.app.FragmentStateManager.createView(FragmentStateManager.java:524)
                                                                                                        at androidx.fragment.app.FragmentStateManager.moveToExpectedState(FragmentStateManager.java:261)
                                                                                                        at androidx.fragment.app.FragmentManager.executeOpsTogether(FragmentManager.java:1890)
                                                                                                        at androidx.fragment.app.FragmentManager.removeRedundantOperationsAndExecute(FragmentManager.java:1814)
                                                                                                        at androidx.fragment.app.FragmentManager.execPendingActions(FragmentManager.java:1751)
                                                                                                        at androidx.fragment.app.FragmentManager$5.run(FragmentManager.java:538)
                                                                                                        at android.os.Handler.handleCallback(Handler.java:938)
                                                                                                        at android.os.Handler.dispatchMessage(Handler.java:99)
                                                                                                        at android.os.Looper.loopOnce(Looper.java:201)
                                                                                                        at android.os.Looper.loop(Looper.java:288)
                                                                                                        at android.app.ActivityThread.main(ActivityThread.java:7870)
                                                                                                        at java.lang.reflect.Method.invoke(Native Method)
                                                                                                        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
                                                                                                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)

0 Answers0