gpt4 book ai didi

使用 AppCompat 项目库进行 Android 单元测试

转载 作者:搜寻专家 更新时间:2023-11-01 08:49:59 25 4
gpt4 key购买 nike

我有一个项目包含一些扩展 ActivityUnitTestCase 的单元测试,但是当我安装 android-support-v7-appcomapt 项目库以使用 ActionBar 时,这些测试已停止工作。我还有一些 ActivityInstrumentationTestCase2 测试,它们工作正常。这是我的设置

protected void setUp() throws Exception {
super.setUp();

Intent intent = new Intent(getInstrumentation().getTargetContext(), MyActivity.class);

startActivity(intent, null, null); // The exception happens here
veActivity = getActivity();

//getInstrumentation().callActivityOnStart(veActivity);
}

如果我不调用 startActivity,veActivity 为空。

java.lang.IllegalArgumentException: android.content.pm.PackageManager$NameNotFoundException: ComponentInfo{mypackage/mypackage.MyActivity}
at android.support.v4.app.NavUtils.getParentActivityName(NavUtils.java:282)
at android.support.v7.app.ActionBarActivityDelegate.onCreate(ActionBarActivityDelegate.java:116)
at android.support.v7.app.ActionBarActivityDelegateICS.onCreate(ActionBarActivityDelegateICS.java:57)
at android.support.v7.app.ActionBarActivity.onCreate(ActionBarActivity.java:98)
at sysnetlab.android.sdc.ui.ViewExperimentActivity.onCreate(ViewExperimentActivity.java:36)
at android.app.Activity.performCreate(Activity.java:5231)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.test.ActivityUnitTestCase.startActivity(ActivityUnitTestCase.java:158)
at sysnetlab.android.sdc.test.myActivityTests.setUp(MyActivityTests.java:34)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:191)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:176)
at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:554)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1701)
Caused by: android.content.pm.PackageManager$NameNotFoundException: ComponentInfo{mypackage/mypackage.MyActivity}
at android.app.ApplicationPackageManager.getActivityInfo(ApplicationPackageManager.java:242)
at android.support.v4.app.NavUtils.getParentActivityName(NavUtils.java:298)
at android.support.v4.app.NavUtils.getParentActivityName(NavUtils.java:279)
... 17 more

我尝试了在 StackOverflow 中找到的许多建议,其中大部分将我引向了这个异常。

android.content.pm.PackageManager$NameNotFoundException: ComponentInfo{mypackage/mypackage.MyActivity}

最佳答案

使用 launchActivity 创建了一个解决方法,并确保启动的 Activity 已完成。手动设置主题,启动等待空闲同步的 Activity ,并在测试后完成 Activity 。

Context context = getInstrumentation().getTargetContext();
context.setTheme(R.style.Theme_AppCompat);
mActivity = launchActivity(context.getPackageName(),
MyActivity.class, null);
getInstrumentation().waitForIdleSync();

// YOUR TESTS
// assertNotNull("The activity cannot be null.", mActivity);

sendKeys(KeyEvent.KEYCODE_BACK);
mActivity.finish();

关于使用 AppCompat 项目库进行 Android 单元测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24499997/

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