【问题标题】:Robolectric+Eclipse Can't find resources?Robolectric+Eclipse 找不到资源?
【发布时间】:2011-03-17 14:15:20
【问题描述】:

我刚刚为我的 Android 应用配置了一个测试项目以使用 Robolectric。 我关注了Eclipse Quick Start。 执行我的第一个简单测试时引发异常。

java.lang.RuntimeException: java.lang.NullPointerException
    at com.xtremelabs.robolectric.res.ResourceLoader.init(ResourceLoader.java:93)
    at com.xtremelabs.robolectric.res.ResourceLoader.getStringValue(ResourceLoader.java:271)
    at com.xtremelabs.robolectric.shadows.ShadowResources.getString(ShadowResources.java:56)
    at android.content.res.Resources.getString(Resources.java)
    at org.xxx.mobile.android.teldir.app.TelephoneDirectoryTest.searchButtonLabelShouldBeGo(TelephoneDirectoryTest.java:22)
    [...]
Caused by: java.lang.NullPointerException
    at com.xtremelabs.robolectric.res.StringResourceLoader.getValue(StringResourceLoader.java:17)
    at com.xtremelabs.robolectric.res.StringArrayResourceLoader.processNode(StringArrayResourceLoader.java:39)
    at com.xtremelabs.robolectric.res.XpathResourceXmlLoader.processResourceXml(XpathResourceXmlLoader.java:27)
    at com.xtremelabs.robolectric.res.DocumentLoader.loadResourceXmlFile(DocumentLoader.java:58)
    at com.xtremelabs.robolectric.res.DocumentLoader.loadResourceXmlDir(DocumentLoader.java:52)
    at com.xtremelabs.robolectric.res.DocumentLoader.loadResourceXmlDir(DocumentLoader.java:39)
    at com.xtremelabs.robolectric.res.ResourceLoader.loadValueResourcesFromDir(ResourceLoader.java:142)
    at com.xtremelabs.robolectric.res.ResourceLoader.loadValueResourcesFromDirs(ResourceLoader.java:136)
    at com.xtremelabs.robolectric.res.ResourceLoader.loadValueResources(ResourceLoader.java:109)
    at com.xtremelabs.robolectric.res.ResourceLoader.init(ResourceLoader.java:85)
    at com.xtremelabs.robolectric.res.ResourceLoader.getStringValue(ResourceLoader.java:271)
    at com.xtremelabs.robolectric.shadows.ShadowResources.getString(ShadowResources.java:56)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at com.xtremelabs.robolectric.bytecode.ShadowWrangler.methodInvoked(ShadowWrangler.java:87)
    at com.xtremelabs.robolectric.bytecode.RobolectricInternals.methodInvoked(RobolectricInternals.java:110)
    at android.content.res.Resources.getString(Resources.java)
    [...]

接下来是测试。

import static org.junit.Assert.assertEquals;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;

import com.xtremelabs.robolectric.RobolectricTestRunner;

@RunWith(RobolectricTestRunner.class)
public class TelephoneDirectoryTest {
    private TelephoneDirectory activity;

    @Before
    public void setUp() {
        activity = new TelephoneDirectory();
    }

    @Test
    public void searchButtonLabelShouldBeGo() throws Exception {
        String goLabel = activity.getResources().getString(R.string.search);
        assertEquals("Go", goLabel);

    }
}

似乎找不到 Android ./res 文件夹。正如指南所说,我将 JUnit 配置指向${workspace_loc:teldir-android}。这使得 Eclipse 可以找到AndroidManifest.xml,避免其他错误,恕我直言。

为了避免这个异常,我还添加了 ./res 文件夹作为我的 Android 应用程序的源文件夹,清理并重新启动所有文件夹,但引发了相同的异常。

我做错了什么?

【问题讨论】:

  • 你也可以用maven试试这个。这是示例代码:github.com/pivotal/RobolectricSample
  • 你能粘贴你的导入代码吗
  • @Sunil 刚刚粘贴了我的导入
  • 您对此有进一步了解吗?我遇到了这个确切的错误。
  • @ahsteele 没有好消息,甚至来自 Robolectric Google Group :(

标签: android eclipse junit4 robolectric


【解决方案1】:

我必须在我的测试运行配置中添加一个环境变量:

ANDROID_HOME=C:/path/to/android-sdks

【讨论】:

  • 这在 Intellij 中对我有用。我已经设置了所有 env 变量...猜我只需要在构建配置中添加引用。谢谢!
【解决方案2】:

第一次使用 Robolectric 设置我的 Eclipse 项目时,我遇到了完全相同的问题(加载资源时出现 RuntimeException),但现在它可以工作了。

我想出了两件事:

  • 如果您收到WARNING: Unable to find path to Android SDK(在低于 1.0-RC1 的版本中可能不会显示该消息),则 ResourceLoader 无法找到您的 SDK 根目录。其中一种方法会查找应指向您的 Android SDK 所在位置的 ANDROID_HOME 环境变量。
  • 最后,有一个bug 如果您的 menu.xml 没有以某种方式格式化,则会导致异常。

在没有 Maven 或 Ant 的情况下使用 Robolectric 是可能的,但可能会有更多错误。 This 视频显示了正确的项目设置。如果它不起作用,请从 Github 获取最新版本(在 Eclipse 中作为 Maven 项目导入,您可能需要 github.com/mosabua/maven-android-sdk-deployer)并开始调试!

【讨论】:

    【解决方案3】:

    如果 ResourceNotFound 仅出现在使用 <array>...</array> 标签定义的字符串数组中,请将其替换为 <string-array>...</string-array>

    使用 robolectric 2.0

    读入:https://groups.google.com/forum/#!topic/robolectric/gQoY0bCguYs

    文档:http://developer.android.com/guide/topics/resources/string-resource.html#StringArray

    【讨论】:

      【解决方案4】:

      这显然是错误的..我必须设置一种蚂蚁方式来做事,因为您无法从 Eclipse 启动 robolectric 测试,因为 Eclipse 工作区仍然无法正确识别链接源单个文件..请参阅我的 alpha 示例:

      https://github.com/shareme/Wabash

      顺便说一句,我遇到了同样的错误……来自 Pivotal Labs 的指示……

      【讨论】:

      • 你是说不像在 Wabash 中那样做(还没有看到),我唯一的方法是直接在同一个 Android 项目中放置一个 ./test 文件夹进行测试,就像在 RoboletricSample 中一样?如果是这样,我想我可以做到……
      【解决方案5】:

      在全新安装 Windows 8 后遇到了这个问题。很可能 Robolectric 找不到 SDK。为了将其指向正确的位置。您可以在您的 Android 项目中生成一个local.properties 文件。这可以在命令行中通过转到项目的根文件夹并发出命令来完成:

      android update project –p c:\project\root\folder
      

      打开local.properties 以验证sdk.dir 是否指向预期的路径。

      还要确保您已下载 android API 级别 8。Robolectric 会在 /path/to/your/sdk/platforms/android-8 目录中查找文件夹。

      【讨论】:

        【解决方案6】:

        您可以在此链接中获取 Jar 文件。只需下载它。在项目中添加 JAR 就是这样。我在我的项目中使用过。有什么疑问可以问我

        http://www.java2s.com/Code/JarDownload/robolectric/robolectric-1.2-8.jar.zip

        【讨论】:

          猜你喜欢
          • 2013-05-15
          • 1970-01-01
          • 1970-01-01
          • 2016-01-30
          • 1970-01-01
          • 2014-05-16
          • 1970-01-01
          • 2015-04-13
          • 1970-01-01
          相关资源
          最近更新 更多