运行 Android Espresso 测试错误您需要使用 Theme.AppCompat 主题(或后代)与此 activity
Running Android Espresso Test errors with You need to use a Theme.AppCompat theme (or descendant) with this activity
嗨,我正在尝试 运行 Espresso 测试,但它失败了,我确定是什么问题或我做错了什么。
当我 运行 测试时,我在控制台中得到以下内容
Running tests
Test running started
java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
at android.support.v7.app.AppCompatDelegateImplV7.createSubDecor(AppCompatDelegateImplV7.java:310)
at android.support.v7.app.AppCompatDelegateImplV7.ensureSubDecor(AppCompatDelegateImplV7.java:279)
at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:253)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:109)
at com.example.Login.LoginActivity.onCreate(LoginActivity.java:54)
at android.app.Activity.performCreate(Activity.java:5990)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
at android.support.test.runner.MonitoringInstrumentation.callActivityOnCreate(MonitoringInstrumentation.java:534)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2311)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2420)
at android.app.ActivityThread.access0(ActivityThread.java:154)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5294)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699)
它说它失败了,因为我需要将 Theme.AppCompat 主题(或后代)与此 activity 一起使用,并显示在堆栈跟踪第 54 行中,即 setContentView(R.layout.activity_login);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
ButterKnife.bind(this);
我已经在应用程序中设置了主题,甚至通过在 activity
上设置主题来确保
<application android:theme="@style/AppTheme">
<activity
android:name=".Login.LoginActivity"
android:theme="@style/AppTheme"
android:windowSoftInputMode="adjustPan"></activity>
我的主题 parent 是什么 "Theme.AppCompat"
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
所以当我使用 Theme.AppCompat
的主题时,我有点困惑为什么它会失败
这也是我从 AppCompactActivity
扩展而来的 activity
public class LoginActivity extends AppCompatActivity
我的 Gradle for androidTest 的依赖项是
androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.1'
androidTestCompile 'com.android.support.test:runner:0.4.1'
androidTestCompile 'com.android.support.test:rules:0.4.1'
androidTestCompile 'com.android.support:appcompat-v7:23.1.1'
androidTestCompile 'com.android.support:design:23.1.1'
androidTestCompile 'junit:junit:4.12'
androidTestCompile "com.crittercism.dexmaker:dexmaker:1.3"
androidTestCompile "com.crittercism.dexmaker:dexmaker-dx:1.3"
androidTestCompile "com.crittercism.dexmaker:dexmaker-mockito:1.3"
androidTestCompile 'org.mockito:mockito-core:1.10.19'
这是我正在尝试的测试 运行
@RunWith(AndroidJUnit4.class)
@LargeTest
public class LoginNavigationForgotPasswordTest {
@Rule
public ActivityTestRule<LoginActivity> mActivityRule = new ActivityTestRule(LoginActivity.class);
@Test
public void testClick() {
// Find Button and Click on it
onView(withId(R.id.login_forgot_password_button)).perform(click());
// Find TextView and verify the correct text that is displayed
onView(withId(R.id.forgot_top_title)).check(matches(withText("Forgot Password")));
}
}
也许我做错了什么很明显。
如有任何帮助,我们将不胜感激。
顺便说一下,当 运行 应用程序运行正常
不确定是什么修复了它,但我做了一些更改并设法获得了一个有效的设置。
- 更改了 "AppTheme" 的名称。我认为当 Gradle 正在构建时,它与其他在 styles.xml 中具有相同名称的库混淆了,所以我将它重命名为更独特的名称。例如
之前:
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
之后:
<style name="MyAppTheme" parent="Theme.AppCompat.Light.NoActionBar">
- 我还将 espresso 更新到更新版本,并确保测试 android 应用注释与应用注释相同。我还排除了一些模块,如此处所述 Why would adding espresso-contrib cause an InflateException?
之前:
androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.1'
androidTestCompile 'com.android.support.test:runner:0.4.1'
androidTestCompile 'com.android.support.test:rules:0.4.1'
androidTestCompile 'com.android.support:appcompat-v7:23.1.1'
androidTestCompile 'com.android.support:design:23.1.1'
androidTestCompile 'junit:junit:4.12'
androidTestCompile "com.crittercism.dexmaker:dexmaker:1.3"
androidTestCompile "com.crittercism.dexmaker:dexmaker-dx:1.3"
androidTestCompile "com.crittercism.dexmaker:dexmaker-mockito:1.3"
androidTestCompile 'org.mockito:mockito-core:1.10.19'
之后:
compile 'com.android.support:support-annotations:23.2.1'
androidTestCompile 'com.android.support:support-annotations:23.2.1'
androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.2'
androidTestCompile ('com.android.support.test.espresso:espresso-contrib:2.2'){
exclude group: 'com.android.support', module: 'appcompat'
exclude group: 'com.android.support', module: 'support-v4'
exclude group: 'com.android.support', module: 'design'
exclude group: 'com.github.afollestad.material-dialogs', module: 'core'
exclude group: 'com.github.afollestad.material-dialogs', module: 'commons'
exclude group: 'com.github.traex.rippleeffect', module:'library'
exclude group: 'com.balysv', module:'material-ripple'
exclude module: 'recyclerview-v7'
}
androidTestCompile ('com.android.support.test:runner:0.5'){
}
androidTestCompile ('com.android.support.test:rules:0.4.1'){
}
androidTestCompile ('junit:junit:4.12')
androidTestCompile "com.crittercism.dexmaker:dexmaker:1.3"
androidTestCompile "com.crittercism.dexmaker:dexmaker-dx:1.3"
androidTestCompile "com.crittercism.dexmaker:dexmaker-mockito:1.3"
androidTestCompile 'org.mockito:mockito-core:1.10.19'
我还添加了以下resolutionStrategy
configurations.all{
resolutionStrategy {
force 'org.hamcrest:hamcrest-core:1.1', 'org.hamcrest:hamcrest-core:1.3'
}
}
事后看来,我也可以通过这种方式添加支持注释
configurations.all{
resolutionStrategy {
force 'com.android.support:support-annotations:23.2.1','org.hamcrest:hamcrest-core:1.1', 'org.hamcrest:hamcrest-core:1.3'
}
}
我遇到了同样的问题,但对我来说,解决方案只是将基于 AppCompat 的主题放在 AndroidTest 的清单中,就像我为 Android 应用程序清单一样。对于我的项目,应用程序清单不用于 Espresso 测试,而是使用 Android 测试清单。
如果您不想弄乱您的主题设置或您的清单,那么有一个更简单的解决方案。
在运行时设置主题,像这样:
@Before
fun setUp() {
context = InstrumentationRegistry.getInstrumentation().targetContext
context.setTheme(R.style.Theme_AppCompat_Light)
}
嗨,我正在尝试 运行 Espresso 测试,但它失败了,我确定是什么问题或我做错了什么。
当我 运行 测试时,我在控制台中得到以下内容
Running tests
Test running started
java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
at android.support.v7.app.AppCompatDelegateImplV7.createSubDecor(AppCompatDelegateImplV7.java:310)
at android.support.v7.app.AppCompatDelegateImplV7.ensureSubDecor(AppCompatDelegateImplV7.java:279)
at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:253)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:109)
at com.example.Login.LoginActivity.onCreate(LoginActivity.java:54)
at android.app.Activity.performCreate(Activity.java:5990)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
at android.support.test.runner.MonitoringInstrumentation.callActivityOnCreate(MonitoringInstrumentation.java:534)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2311)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2420)
at android.app.ActivityThread.access0(ActivityThread.java:154)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5294)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699)
它说它失败了,因为我需要将 Theme.AppCompat 主题(或后代)与此 activity 一起使用,并显示在堆栈跟踪第 54 行中,即 setContentView(R.layout.activity_login);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
ButterKnife.bind(this);
我已经在应用程序中设置了主题,甚至通过在 activity
上设置主题来确保 <application android:theme="@style/AppTheme">
<activity
android:name=".Login.LoginActivity"
android:theme="@style/AppTheme"
android:windowSoftInputMode="adjustPan"></activity>
我的主题 parent 是什么 "Theme.AppCompat"
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
所以当我使用 Theme.AppCompat
的主题时,我有点困惑为什么它会失败这也是我从 AppCompactActivity
扩展而来的 activitypublic class LoginActivity extends AppCompatActivity
我的 Gradle for androidTest 的依赖项是
androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.1'
androidTestCompile 'com.android.support.test:runner:0.4.1'
androidTestCompile 'com.android.support.test:rules:0.4.1'
androidTestCompile 'com.android.support:appcompat-v7:23.1.1'
androidTestCompile 'com.android.support:design:23.1.1'
androidTestCompile 'junit:junit:4.12'
androidTestCompile "com.crittercism.dexmaker:dexmaker:1.3"
androidTestCompile "com.crittercism.dexmaker:dexmaker-dx:1.3"
androidTestCompile "com.crittercism.dexmaker:dexmaker-mockito:1.3"
androidTestCompile 'org.mockito:mockito-core:1.10.19'
这是我正在尝试的测试 运行
@RunWith(AndroidJUnit4.class)
@LargeTest
public class LoginNavigationForgotPasswordTest {
@Rule
public ActivityTestRule<LoginActivity> mActivityRule = new ActivityTestRule(LoginActivity.class);
@Test
public void testClick() {
// Find Button and Click on it
onView(withId(R.id.login_forgot_password_button)).perform(click());
// Find TextView and verify the correct text that is displayed
onView(withId(R.id.forgot_top_title)).check(matches(withText("Forgot Password")));
}
}
也许我做错了什么很明显。
如有任何帮助,我们将不胜感激。
顺便说一下,当 运行 应用程序运行正常
不确定是什么修复了它,但我做了一些更改并设法获得了一个有效的设置。
- 更改了 "AppTheme" 的名称。我认为当 Gradle 正在构建时,它与其他在 styles.xml 中具有相同名称的库混淆了,所以我将它重命名为更独特的名称。例如
之前:
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
之后:
<style name="MyAppTheme" parent="Theme.AppCompat.Light.NoActionBar">
- 我还将 espresso 更新到更新版本,并确保测试 android 应用注释与应用注释相同。我还排除了一些模块,如此处所述 Why would adding espresso-contrib cause an InflateException?
之前:
androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.1'
androidTestCompile 'com.android.support.test:runner:0.4.1'
androidTestCompile 'com.android.support.test:rules:0.4.1'
androidTestCompile 'com.android.support:appcompat-v7:23.1.1'
androidTestCompile 'com.android.support:design:23.1.1'
androidTestCompile 'junit:junit:4.12'
androidTestCompile "com.crittercism.dexmaker:dexmaker:1.3"
androidTestCompile "com.crittercism.dexmaker:dexmaker-dx:1.3"
androidTestCompile "com.crittercism.dexmaker:dexmaker-mockito:1.3"
androidTestCompile 'org.mockito:mockito-core:1.10.19'
之后:
compile 'com.android.support:support-annotations:23.2.1'
androidTestCompile 'com.android.support:support-annotations:23.2.1'
androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.2'
androidTestCompile ('com.android.support.test.espresso:espresso-contrib:2.2'){
exclude group: 'com.android.support', module: 'appcompat'
exclude group: 'com.android.support', module: 'support-v4'
exclude group: 'com.android.support', module: 'design'
exclude group: 'com.github.afollestad.material-dialogs', module: 'core'
exclude group: 'com.github.afollestad.material-dialogs', module: 'commons'
exclude group: 'com.github.traex.rippleeffect', module:'library'
exclude group: 'com.balysv', module:'material-ripple'
exclude module: 'recyclerview-v7'
}
androidTestCompile ('com.android.support.test:runner:0.5'){
}
androidTestCompile ('com.android.support.test:rules:0.4.1'){
}
androidTestCompile ('junit:junit:4.12')
androidTestCompile "com.crittercism.dexmaker:dexmaker:1.3"
androidTestCompile "com.crittercism.dexmaker:dexmaker-dx:1.3"
androidTestCompile "com.crittercism.dexmaker:dexmaker-mockito:1.3"
androidTestCompile 'org.mockito:mockito-core:1.10.19'
我还添加了以下resolutionStrategy
configurations.all{
resolutionStrategy {
force 'org.hamcrest:hamcrest-core:1.1', 'org.hamcrest:hamcrest-core:1.3'
}
}
事后看来,我也可以通过这种方式添加支持注释
configurations.all{
resolutionStrategy {
force 'com.android.support:support-annotations:23.2.1','org.hamcrest:hamcrest-core:1.1', 'org.hamcrest:hamcrest-core:1.3'
}
}
我遇到了同样的问题,但对我来说,解决方案只是将基于 AppCompat 的主题放在 AndroidTest 的清单中,就像我为 Android 应用程序清单一样。对于我的项目,应用程序清单不用于 Espresso 测试,而是使用 Android 测试清单。
如果您不想弄乱您的主题设置或您的清单,那么有一个更简单的解决方案。
在运行时设置主题,像这样:
@Before
fun setUp() {
context = InstrumentationRegistry.getInstrumentation().targetContext
context.setTheme(R.style.Theme_AppCompat_Light)
}