MVVM:尝试使用LiveData加载数据时,ViewModel为null

问题描述 投票:0回答:2

因此,我正在尝试构建一个简单的MVVM接口,以将由ViewModel检索的数据库中的旅行加载到我的TripFragment中。但是,我一直收到此错误,说我的TripViewModel为null:

Attempt to invoke virtual method 'void androidx.lifecycle.LiveData.observe(androidx.lifecycle.LifecycleOwner, androidx.lifecycle.Observer)' on a null object reference

我似乎无法弄清为什么它认为它为空。我认为问题出在TripViewModel中,并且与它从AndroidViewModel继承的事实以及我在构造函数中传递应用程序上下文有关。

class TripFragment:Fragment()

private var tripViewModel: TripViewModel? = null
private var textViewTripName: TextView? = null

override fun onCreateView(
    inflater: LayoutInflater,
    container: ViewGroup?,
    savedInstanceState: Bundle?
): View? {
    val view =
        inflater.inflate(R.layout.fragment_trip, container, false)

    val recyclerView: RecyclerView = view.recycler_view
    recyclerView.layoutManager = LinearLayoutManager(context)
    recyclerView.setHasFixedSize(true)

    val adapter = TripAdapter()
    recyclerView.adapter = adapter

    tripViewModel = ViewModelProvider(this).get(TripViewModel(activity!!.application)::class.java)


    // This is the line where it crashes, it never executes past this
    tripViewModel!!.getAll().observe(viewLifecycleOwner, Observer<List<Trip>> {
        fun onChanged(trips: List<Trip>) {
            adapter.setTrips(trips)
            Log.d("TripFragment", "Went through observer")
        }
    })

    return view
}

类TripViewModel(应用程序:应用程序):AndroidViewModel(应用程序)

private var tripRepository: TripRepository = TripRepository(application)
private var allTrips: LiveData<List<Trip>> = getAll()

fun insert(trip: Trip) {
    tripRepository.insert(trip)
}

fun update(trip: Trip) {
    tripRepository.update(trip)
}

fun delete(trip: Trip) {
    tripRepository.delete(trip)
}

fun clear() {
    tripRepository.clear()
}

fun getAll(): LiveData<List<Trip>> {
    return allTrips
}

类TripRepository(应用程序:应用程序)

private lateinit var tripDao: TripDao
private lateinit var allTrips: LiveData<List<Trip>>

init {
    CoroutineScope(IO).launch {
        tripDao = AppDatabase.getDatabase(application.applicationContext).tripDao()
        allTrips = tripDao.getAll()
    }
}

fun insert(trip: Trip) {
    CoroutineScope(IO).launch {
        tripDao.insert(trip)
    }
}

fun update(trip: Trip) {
    CoroutineScope(IO).launch {
        tripDao.update(trip)
    }
}

fun delete(trip: Trip) {
    CoroutineScope(IO).launch {
        tripDao.delete(trip)
    }
}

fun clear() {
    CoroutineScope(IO).launch {
        tripDao.clear()
    }
}

fun getAll(): LiveData<List<Trip>> {
    return allTrips
}

旅行实体

@Entity
data class Trip(var title: String, var startDate: String, var endDate: String?) {

    @PrimaryKey(autoGenerate = true)
    var tid: Long = 0
}

编辑:我已经打印了一堆调试日志,并在TripRepository的这一行中指出了错误。

init {
    CoroutineScope(IO).launch {
        // tripDao is never assigned properly, 
        tripDao = AppDatabase.getDatabase(application.applicationContext).tripDao()
        allTrips = tripDao.getAll()
    }
}

tripDao = AppDatabase.getDatabase(application.applicationContext).tripDao()导致错误,该错误将tripDao转换为空变量。问题与我如何获取数据库有关,因此我在下面附加了AppDatabase类。

@Database(entities = [Trip::class], version = 2, exportSchema = false)
abstract class AppDatabase : RoomDatabase() {

    abstract fun tripDao(): TripDao

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

        fun getDatabase(context: Context): AppDatabase {
            val tempInstance = INSTANCE
            if (tempInstance != null) {
                Log.d("AppDatabase", "Returning existing database")
                return tempInstance
            }
            synchronized(this) {
                val instance = Room.databaseBuilder(
                    context.applicationContext,
                    AppDatabase::class.java,
                    "tripweaver_database"
                )
                    .fallbackToDestructiveMigration()
                    .build()
                INSTANCE = instance
                Log.d("AppDatabase", "Returning new database")
                return instance
            }
        }
    }
}
android kotlin mvvm android-room android-livedata
2个回答
0
投票

在您的班级TripViewModel中,您拥有:

private var allTrips: LiveData<List<Trip>> = getAll()

fun getAll(): LiveData<List<Trip>> {
    return allTrips
}

所以您在这里有一个圆形排列。 allTrips的初始值分配为getAll()返回的值,但是在getAll()分配之前它正在调用allTrips。因此,您找到了一种将null分配给非null的方法!

似乎您打算放allTrips = tripRepository.getAll()


0
投票

我找不到修复数据库访问代码的解决方案,因此我根据Google在Codelabs上的体系结构组件教程重新构造了整个代码库。当然,这足以帮助我解决遇到的问题。将来需要本教程的人的链接:https://codelabs.developers.google.com/codelabs/android-room-with-a-view-kotlin/#13

© www.soinside.com 2019 - 2024. All rights reserved.