Android Room Live Data Listener使应用程序崩溃

时间:2018-01-12 18:09:45

标签: android observers android-room android-livedata

我为我的Android应用程序实现了一个房间数据库。它与测试项异步填充数据库,这似乎到目前为止工作。现在我想实现一个监听器,如果数据发生变化,它会动态更新UI。但是我的听众会让应用程序在启动时崩溃。这是我在活动中的倾听者:

    LiveData<List<Cat>> CatLiveData = Manager.getDB().getCatDAO().getCats();
    CatLiveData. observe(this, new Observer<List<Cat>>() {
        @Override
        public void onChanged(@Nullable List<Cat> Cats) {
            //Update your UI here.
            binding.myControl.setText(Cats.get(0).getsName().toString());
        }
    });

它应该获取Cats-list的第一个元素并返回其name属性。 myControl应该显示这个属性。检查null不能解决崩溃问题。

非常感谢!

编辑: 这是LOgCat

01-23 22:18:00.863 9633-9633 /?我/艺术:延迟启用-Xcheck:jni 01-23 22:18:00.931 9633-9640 /?我/艺术:调试器不再有效 01-23 22:18:00.931 9633-9640 /?我/艺术:开始阻止GC仪器 01-23 22:18:01.377 9633-9633 /? W / System:ClassLoader引用未知路径:/mnt/expand/27c5fc5f-9c8e-4173-8379-f18e8d4cf183/app/com.example.erik.test-1/lib/arm 01-23 22:18:01.411 9633-9633 /? I / InstantRun:启动即时运行服务器:是主进程 01-23 22:18:01.612 9633-9633 /? W / art:在Android 4.1之前,方法android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter,android.content.res.ColorStateList,android.graphics.PorterDuff $ Mode)会有错误地覆盖了android.graphics.drawable.Drawable中的package-private方法 01-23 22:18:02.213 9633-9652 / com.example.erik.test I / Adreno-EGL :: EGL 1.4 QUALCOMM build:Nondeterministic_AU_msm8974_LA.BF.1.1.3_RB1__release_AU(I389a668de0)                                                                  OpenGL ES着色器编译器版本:E031.28.00.04                                                                  建造日期:2015年10月24日星期六                                                                  当地分公司:mybranch15621148                                                                  远程分支:quic / LA.BF.1.1.3_rb1.3                                                                  本地补丁:无                                                                  重建分支:没有 01-23 22:18:02.217 9633-9652 / com.example.erik.test I / OpenGLRenderer:初始化的EGL,版本1.4 01-23 22:18:02.217 9633-9652 / com.example.erik.test D / OpenGLRenderer:交换行为1 01-23 22:18:02.267 9633-9633 / com.example.erik.test D / AndroidRuntime:关闭VM 01-23 22:18:02.268 9633-9633 / com.example.erik.test E / AndroidRuntime:FATAL EXCEPTION:main                                                                      处理:com.example.erik.test,PID:9633                                                                      java.lang.NoSuchMethodError:没有虚方法iteratorWithAdditions()Landroid / arch / core / internal / SafeIterableMap $ ListIterator;在类Landroid / arch / core / internal / SafeIterableMap;或其超级类('android.arch.core.internal.SafeIterableMap'的声明出现在/mnt/expand/27c5fc5f-9c8e-4173-8379-f18e8d4cf183/app/com.example.erik.test-1/split_lib_dependencies_apk.apk )                                                                          在android.arch.lifecycle.LiveData.dispatchingValue(LiveData.java:145)                                                                          在android.arch.lifecycle.LiveData.setValue(LiveData.java:293)                                                                          在android.arch.lifecycle.LiveData $ 2.run(LiveData.java:108)                                                                          在android.os.Handler.handleCallback(Handler.java:751)                                                                          在android.os.Handler.dispatchMessage(Handler.java:95)                                                                          在android.os.Looper.loop(Looper.java:154)                                                                          在android.app.ActivityThread.main(ActivityThread.java:6186)                                                                          at java.lang.reflect.Method.invoke(Native Method)                                                                          在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:889)                                                                          在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:779)

1 个答案:

答案 0 :(得分:-1)

到目前为止,我已经从sratch重建了应用程序 - 我从未找到它无效的确切原因。最有可能的是,混乱的结构有许多初学者的缺陷。有关帮助我解决问题的房间数据库的非常好且简单的手册,我可以推荐这个Codelab:

https://codelabs.developers.google.com/codelabs/android-room-with-a-view/#0