gpt4 book ai didi

android - 运行 Android Espresso 测试错误,您需要在此 Activity 中使用 Theme.AppCompat 主题(或后代)

转载 作者:太空宇宙 更新时间:2023-11-03 13:50:06 24 4
gpt4 key购买 nike

您好,我正在尝试运行 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.access$900(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)

它说它失败了,因为我需要在此 Activity 中使用 Theme.AppCompat 主题(或后代),并显示在堆栈跟踪第 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>

我的主题父级是“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")));
}

}

也许我做错了什么很明显。

如有任何帮助,我们将不胜感激。

顺便说一句,应用程序在运行时运行良好

最佳答案

不确定是什么修复了它,但我做了一些更改并设法获得了一个有效的设置。

  1. 更改了“AppTheme”的名称。我认为 Gradle 在构建时会与 styles.xml 中具有相同名称的其他库混淆,因此我将其重命名为更独特的名称。例如

之前:

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">

之后:

<style name="MyAppTheme" parent="Theme.AppCompat.Light.NoActionBar">
  1. 我还将 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'
}

}

关于android - 运行 Android Espresso 测试错误,您需要在此 Activity 中使用 Theme.AppCompat 主题(或后代),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36086776/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com