【问题标题】:Android Room: 'NoSuchMethodError: No interface method' for Dao methods that update, with Room.inMemoryDatabaseBuilderAndroid Room:使用 Room.inMemoryDatabaseBuilder 更新的 Dao 方法的“NoSuchMethodError:无接口方法”
【发布时间】:2018-02-05 20:28:27
【问题描述】:

我在 Android 上使用 Room。因此,我的数据模型类具有正确的 Room 注释和一个公共构造函数。它使用构建器模式并尝试保持不变。我是here.

然后我的@Dao-annotated 接口与@Query@Update@Insert 注释紧挨着所有方法声明。就在here.

最后,我得到了我的 Dao 测试类,它使用了Room.inMemoryDatabaseBuilder。是here

因此,当我在GoalsDaoTest 中运行测试时,除了两个之外,它们都通过了:一个使用注释为@Update 的Dao 方法,另一个其Dao 方法在其@Query 中使用SQL UPDATE 命令。

这是我的测试设置和拆卸,使用Room.inMemoryDatabaseBuilder

@Before
public void initDb() {     
    mDatabase = Room.inMemoryDatabaseBuilder(InstrumentationRegistry.getContext(),
            SelfCareDatabase.class).build();
}

@After
public void closeDb() {
    mDatabase.close();
}

这是第一个失败的测试:

@Test
public void updateArchivedAndGetById() {
    // When inserting a goal
    mDatabase.goalDao().insertGoal(GOAL);

    // When the goal is updated
    mDatabase.goalDao().updateArchived(GOAL.getId(), true);

    // When getting the goal by id from the database
    Goal loaded = mDatabase.goalDao().getGoalById(ID);

    // The loaded data contains the expected values
    assertGoal(loaded, GOAL.getId(), GOAL.getTitle(), GOAL.getInterval(), GOAL.getPolarity(), true, GOAL.getTouched());
}

这是我运行它时产生的错误:

java.lang.NoSuchMethodError: No interface method updateArchived(Ljava/lang/String;Z)V in class Lcom/beatboxchad/android/selfcaredashboard/data/source/local/GoalsDao; or its super classes (declaration of 'com.beatboxchad.android.selfcaredashboard.data.source.local.GoalsDao' appears in /data/app/com.beatboxchad.android.selfcaredashboard-1/base.apk)
at com.beatboxchad.android.selfcaredashboard.data.source.local.GoalsDaoTest.updateArchivedAndGetById(GoalsDaoTest.java:155)
at java.lang.reflect.Method.invoke(Native Method)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at android.support.test.internal.runner.junit4.statement.RunBefores.evaluate(RunBefores.java:80)
at android.support.test.internal.runner.junit4.statement.RunAfters.evaluate(RunAfters.java:61)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runners.Suite.runChild(Suite.java:128)
at org.junit.runners.Suite.runChild(Suite.java:27)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at org.junit.runner.JUnitCore.run(JUnitCore.java:115)
at android.support.test.internal.runner.TestExecutor.execute(TestExecutor.java:58)
at android.support.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:375)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1939)

然而,该方法已在GoalsDao 类中正确声明和注释:

/**
 * Update the Archived status of a goal
 *
 * @param goalId    id of the goal
 * @param archived status to be updated
 */
@Query("UPDATE goals SET archived = :archived WHERE entryid = :goalId")
void updateArchived(String goalId, boolean archived);

这是另一个测试:

@Test
public void updateGoalAndGetById() {
    // When inserting a goal
    mDatabase.goalDao().insertGoal(GOAL);

    // When the goal is updated
    Goal updatedGoal = new Goal.Builder(ID)
            .setTitle(TITLE2)
            .setPolarity(POLARITY2)
            .setInterval(INTERVAL2)
            .setArchived(ARCHIVED2)
            .setTouched(TOUCHED2)
            .build();

    mDatabase.goalDao().updateGoal(updatedGoal);

    // When getting the goal by id from the database
    Goal loaded = mDatabase.goalDao().getGoalById(ID);

    // The loaded data contains the expected values
    assertGoal(loaded, ID, TITLE2, INTERVAL2, POLARITY2, ARCHIVED2, TOUCHED2);
}

这是它的错误:

java.lang.NoSuchMethodError: No interface method updateGoal(Lcom/beatboxchad/android/selfcaredashboard/data/Goal;)I in class Lcom/beatboxchad/android/selfcaredashboard/data/source/local/GoalsDao; or its super classes (declaration of 'com.beatboxchad.android.selfcaredashboard.data.source.local.GoalsDao' appears in /data/app/com.beatboxchad.android.selfcaredashboard-1/base.apk)
at com.beatboxchad.android.selfcaredashboard.data.source.local.GoalsDaoTest.updateGoalAndGetById(GoalsDaoTest.java:140)
at java.lang.reflect.Method.invoke(Native Method)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at android.support.test.internal.runner.junit4.statement.RunBefores.evaluate(RunBefores.java:80)
at android.support.test.internal.runner.junit4.statement.RunAfters.evaluate(RunAfters.java:61)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runners.Suite.runChild(Suite.java:128)
at org.junit.runners.Suite.runChild(Suite.java:27)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at org.junit.runner.JUnitCore.run(JUnitCore.java:115)
at android.support.test.internal.runner.TestExecutor.execute(TestExecutor.java:58)
at android.support.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:375)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1939)

然而!迷惑我!是什么疯狂导致计算机撒谎?目标道.java 摘录:

/**
 * Update a goal.
 *
 * @param goal goal to be updated
 * @return the number of goals updated. This should always be 1.
 */
@Update
int updateGoal(Goal goal);

我已经用尽了这一项上所有容易实现的目标。一切似乎都符合文档(尽管我怀疑我的不可变 builder'd Goal 类),并且我尝试遵循以下地方给出的建议:

Why Room entities don't work with immutable properties in Android

Espresso Test Failing: No interface method trackUsage() in UsageTracker.java

Room Persistence Library run time exception when calling Rooms inMemoryBuilder method

但我没有使用 Kotlin,也没有使用 @Relation 注释,即使我在 build.gradle 文件中使用了各种不同的版本,它仍然坏掉了。好的一面是,我学到了很多关于那个无聊的部分。 tl;博士我很难过。

以防万一它是有用的上下文,我的应用程序强烈基于来自https://github.com/googlesamples/android-architecture 的 todo‑mvvm‑databinding 示例。我提到这一点是因为示例中的所有内容都有效,但是我增加了一些复杂性,并且可能在此过程中犯了一个更有经验的人会认识到的错误。特别是,我修改了 @Entity 目标类(示例应用程序中的任务),试图保持它不可变并添加构建器模式和几个属性。我也没有完成对它的修改——touched 即将成为它自己的表。

我也不擅长将接口和一堆注释转换为代码的故障排除。任何关于在哪里解决这个问题的建议都与它会导致的答案一样有用。我是 Android Studio、Java 和整个构建过程的新手。

谢谢!

【问题讨论】:

  • 只是为了确保,您是否已将 annotationProcessor 添加到您的 build.gradle 文件中?
  • 我做到了!事实上,我在项目中添加了 kotlin 依赖项并将其改为kapt(我没有保留这些更改)。这可能已经修复了它,但它破坏了数据绑定(或其他东西——由于各种问题,我收到关于未找到绑定类的相同错误消息——通常是布局文件中的语法错误),因此我不能根本无法运行任何测试,因为它无法编译。我没有尝试将kapt 用于Room 和annotationProcessor 用于数据绑定的东西,明确地。不过,我不知道该向annotationProcessor 提供什么论据。

标签: java android junit4 android-room


【解决方案1】:

在 android studio 中使缓存失效并重启有帮助

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-01
    • 2020-08-22
    • 2022-06-15
    • 1970-01-01
    相关资源
    最近更新 更多