android - Robolectric: “AndroidManifest.xml not found” 和 "Unable to find resource ID #0x7f09001b"

标签 android compiler-errors android-manifest robolectric

我正在使用 Roboletric 运行一些测试,但我遇到了一个我无法解决的问题。 当我运行测试时,“AndroidManifest”出现以下错误:

WARNING: No manifest file found at .\AndroidManifest.xml.

Falling back to the Android OS resources only. To remove this warning, annotate your test class with @Config(manifest=Config.NONE).

No such manifest file: .\AndroidManifest.xml

我已经尝试了这些失败的解决方案:

@Config (manifest = Config.DEFAULT_MANIFEST_NAME)

@Config(manifest = Config.NONE, constants = BuildConfig.class, sdk = 26)

@Config( constants = BuildConfig.class, manifest="src/main/AndroidManifest.xml", sdk = 26 )

执行过程中的另一个错误是:

android.content.res.Resources$NotFoundException: Unable to find resource ID #0x7f09001b in packages [android, org.robolectric.default]

...

at

com.example.robertoassad.alltestsmerge.MainActivity.onCreate(MainActivity.java:52)

这一行有错误的是下面的代码:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

具体在:setContentView(R.layout.activity_main);

对我来说,我认为这个问题没有意义......

详情:

  • 测试类在文件夹中:app\src\test\java\com\example\robertoassad

  • 测试是: @RunWith(RobolectricTestRunner.class) 公共(public)类 Roboletric { @测试 public void clickingLogin_shouldStartLoginActivity() { MainActivity activity = Robolectric.setupActivity(MainActivity.class); activity.findViewById(R.id.button2).performClick();

            Intent expectedIntent = new Intent(activity, SecondActivity.class);
            Intent actual = ShadowApplication.getInstance().getNextStartedActivity();
            assertEquals(expectedIntent.getComponent(), actual.getComponent());
         }
    

最佳答案

我遇到了和你遇到的问题类似的问题。 post by jongerrish在 Robolectric GitHub Issue 上关于这个的问题为我解决了这个问题。

对我有用的答案是在我模块的 build.gradle 文件中添加一个 testOptions block :

testOptions {
    unitTests {
        includeAndroidResources = true
    }
}

添加此 block 后,我的测试能够运行并访问字符串资源。

关于android - Robolectric: “AndroidManifest.xml not found” 和 "Unable to find resource ID #0x7f09001b",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47456711/

相关文章:

android - 找不到参数的方法 path()

java - 实现 ListView 的上下文菜单?

android - 传入消息广播接收器不工作

android - 如何为文件浏览器应用程序 "myfiles"注册备用 mp3 查看器?

android - 如何确定用户是否取消了 Google Play 订阅?

android - 防止媒体播放器同时播放多个声音

xcode - 如何禁用 "Multiple methods named ... found with mismatched result, parameter type or attributes"

c++ - VC++。net中执行文件读取代码时出错

c# - 命令行 MSBuild 无法编译代码

java - 我的 manifest.xml 文件中出现错误 "The uses SDK element must be a direct child of the manifest root element"