繁体   English   中英

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

[英]MVVM: ViewModel is null when trying to load data using LiveData

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

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

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

类 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
            }
        }
    }
}

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

在您的课程TripViewModel ,您有:

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

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

所以你在这里有一个圆形的安排。 allTrips被分配了一个初始值,无论getAll()返回什么,但它在分配allTrips之前调用getAll() 所以你找到了一种将空值分配给不可空值的方法!

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM