gpt4 book ai didi

android - ActivityUnitTestCase 和 startActivity 与 ActionBarActivity

转载 作者:IT老高 更新时间:2023-10-28 23:21:23 24 4
gpt4 key购买 nike

我尝试测试一个使用 ActionBarActivity(来自 appcompat 库)的 Activity。我需要一个自定义应用程序,以便能够操纵 DI 系统来加载我的测试服务而不是实际服务。

如果我编写了测试并调用 startActivity,我会收到以下错误:

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

如果我调用launchActivityWithIntent Activity 启动没有任何问题,但它使用的是我的真实应用程序类而不是模拟应用程序类。有什么想法可以解决这个问题,或者在调用应用程序的 onCreate 之后但在我的仪器测试中调用我的 Activity 的 onCreate 之前如何执行代码?

最佳答案

接受的答案在我的情况下不起作用,但在 ActicityUnitTestCase 子类中包含一些对我有用的东西:

@Override
public void setUp(){
ContextThemeWrapper context = new ContextThemeWrapper(getInstrumentation().getTargetContext(), R.style.AppTheme);
setActivityContext(context);
}

关于android - ActivityUnitTestCase 和 startActivity 与 ActionBarActivity,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22364433/

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