gpt4 book ai didi

android - 使用 Robolectric 测试 ActionBarActivity 时出错

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:04:05 26 4
gpt4 key购买 nike

我正在使用 Robolectric 测试我的应用程序中的 Activity 。该 Activity 扩展了 ActionBarActivity。当我测试 Activity 时出现错误:

IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.

我的测试环境是这样的:

  • Android Studio 0.8.6
  • Robolectric 2.4-快照
  • 支持-v4 库版本 20.0
  • appcompat-v7 版本 20.0

现在开始上课。 Activity :

public class FilterActivity extends ActionBarActivity

测试类:

@Config(emulateSdk = 18, manifest = "src/main/AndroidManifest.xml")
@RunWith(RobolectricTestRunner.class)
public class FilterActivityTest
{
private FilterActivity activity;

@Before
public void setUp() throws Exception
{
activity = Robolectric.buildActivity(FilterActivity.class).create().get();
}

@After
public void tearDown() throws Exception
{
activity.finish();
}

@Test
public void testOnCreate()
{

}
}

我的 styles.xml 来自 values 文件夹:

<style name="AppBaseTheme" parent="@style/Theme.AppCompat.Light.DarkActionBar">

</style>

堆栈跟踪:

java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
at android.support.v7.app.ActionBarActivityDelegate.onCreate(ActionBarActivityDelegate.java:110)
at android.support.v7.app.ActionBarActivityDelegateICS.onCreate(ActionBarActivityDelegateICS.java:57)
at android.support.v7.app.ActionBarActivity.onCreate(ActionBarActivity.java:99)
at ro.cursurideschimb.user.FilterActivity.onCreate(FilterActivity.java:97)
at android.app.Activity.performCreate(Activity.java:5133)
at org.fest.reflect.method.Invoker.invoke(Invoker.java:112)
at org.robolectric.util.ActivityController$1.run(ActivityController.java:113)
at org.robolectric.shadows.ShadowLooper.runPaused(ShadowLooper.java:265)
at org.robolectric.util.ActivityController.create(ActivityController.java:110)
at org.robolectric.util.ActivityController.create(ActivityController.java:120)
at ro.cursurideschimb.user.FilterActivityTest.setUp(FilterActivityTest.java:38)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:24)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
at org.robolectric.RobolectricTestRunner$2.evaluate(RobolectricTestRunner.java:267)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
at org.robolectric.RobolectricTestRunner$1.evaluate(RobolectricTestRunner.java:194)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.junit.runner.JUnitCore.run(JUnitCore.java:160)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)

感谢您的帮助。

最佳答案

我有同样的问题,我相信这是由使用 @style/Theme.AppCompat.Light.DarkActionBar 引起的对比Theme.AppCompat.Light.DarkActionBar

<style name="Theme.MyApp" parent="Base.Theme.MyApp">
</style>

<style name="Base.Theme.MyApp" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorPrimary">@color/charcoal_gray</item>
<item name="colorPrimaryDark">@color/black</item>
<item name="colorAccent">@color/white</item>
<item name="android:windowBackground">@color/black</item>
</style>

此外,可能值得一提的是,在使用较新版本的支持库时,建议切换 ActionBarActivityAppCompatActivity因为前者现在已被弃用。

如果您使用的是较旧的目标 SDK,这可能适用于您:ActionBarCompat: java.lang.IllegalStateException: You need to use a Theme.AppCompat

关于android - 使用 Robolectric 测试 ActionBarActivity 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25653873/

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