- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
更新#1:更多信息添加到这篇文章的末尾
我是 Android 开发和测试的新手。
我有 3 个 Espresso 测试。第一个测试通过,但第二个不会运行,因为在第二个测试之前调用 setUp() 方法中的 getActivity() 总是失败:
Blockquote java.lang.RuntimeException: Could not launch intent Intent { act=android.intent.action.MAIN flg=0x10000000 cmp=my.packagename/.ActivityMain } within 45 seconds. ...
第三个测试通过。
我在创建时没有任何长时间运行的操作、动画或网络调用。我可以在我的应用程序中手动点击所有菜单项,毫无问题地重复测试流程。
由于某种原因,第一个测试在第二个测试之前“中断”了 setUp() 中的下一个 getActivity() 调用。以下所有测试(在第二次测试之后)都将正常运行。
我找到了 similar问题,但看起来它没有得到解决,它有一些不同的问题。
测试代码:
import static com.google.android.apps.common.testing.ui.espresso.Espresso.onData;
import static com.google.android.apps.common.testing.ui.espresso.Espresso.onView;
import static com.google.android.apps.common.testing.ui.espresso.Espresso.openActionBarOverflowOrOptionsMenu;
import static com.google.android.apps.common.testing.ui.espresso.action.ViewActions.click;
import static com.google.android.apps.common.testing.ui.espresso.assertion.ViewAssertions.matches;
import static com.google.android.apps.common.testing.ui.espresso.matcher.ViewMatchers.isDisplayed;
import static com.google.android.apps.common.testing.ui.espresso.matcher.ViewMatchers.withId;
import static com.google.android.apps.common.testing.ui.espresso.matcher.ViewMatchers.withText;
import static org.hamcrest.Matchers.allOf;
import static org.hamcrest.Matchers.instanceOf;
import static org.hamcrest.Matchers.is;
import net.humblegames.bodylasticscalculator.ActivityMain;
import net.humblegames.bodylasticscalculator.R;
import net.humblegames.bodylasticscalculator.applogic.BandSystem;
import org.hamcrest.BaseMatcher;
import org.hamcrest.Description;
import org.hamcrest.Matcher;
import org.junit.After;
import org.junit.Before;
import android.app.Activity;
import android.test.ActivityInstrumentationTestCase2;
import android.util.Log;
public class MenuNavigationTestExperiment extends
ActivityInstrumentationTestCase2<ActivityMain> {
private String TAG = getClass().getSimpleName();
private Activity activity;
public MenuNavigationTestExperiment() {
super(ActivityMain.class);
}
@Before
public void setUp() throws Exception {
Log.d(TAG, "SETUP");
activity = getActivity();
super.setUp();
}
@After
public void tearDown() throws Exception {
Log.d(TAG, "TEARDOWN");
super.tearDown();
}
public void testFirst() {
Log.d(TAG, "testFirst");
clickMenuItem("Select band system");
onData(allOf(is(instanceOf(BandSystem.class)), hasName("MMA Training")))
.onChildView(withId(R.id.label)).perform(click());
clickMenuItem("About");
onView(withId(R.id.about_webview)).check(matches(isDisplayed()));
}
public void testSecond() {
Log.d(TAG, "testSecond");
// this test will not run
}
public void testThird() {
Log.d(TAG, "testThird");
// this test will pass
}
// ------------------ HELPER METHODS ---------------------------------------
private void clickMenuItem(String menuItem) {
Log.d(TAG, "clickMenuItem");
openActionBarOverflowOrOptionsMenu(getInstrumentation()
.getTargetContext());
onView(withText(menuItem)).perform(click());
}
private Matcher<BandSystem> hasName(final String name) {
Log.d(TAG, "hasName");
return new BaseMatcher<BandSystem>() {
@Override
public boolean matches(final Object item) {
final BandSystem foo = (BandSystem) item;
return name == foo.getName();
}
@Override
public void describeTo(final Description description) {
description.appendText("getName should return ").appendValue(
name);
}
};
}
}
错误轨迹:
java.lang.RuntimeException: Could not launch intent Intent { act=android.intent.action.MAIN flg=0x10000000 cmp=net.humblegames.bodylasticscalculator/.ActivityMain } within 45 seconds. Perhaps the main thread has not gone idle within a reasonable amount of time? There could be an animation or something constantly repainting the screen. Or the activity is doing network calls on creation? See the threaddump logs. For your reference the last time the event queue was idle before your activity launch request was 1395582828351 and and now the last time the queue went idle was: 1395582830169. If these numbers are the same your activity might be hogging the event queue.
at com.google.android.apps.common.testing.testrunner.GoogleInstrumentation.startActivitySync(GoogleInstrumentation.java:277)
at android.test.InstrumentationTestCase.launchActivityWithIntent(InstrumentationTestCase.java:119)
at android.test.InstrumentationTestCase.launchActivity(InstrumentationTestCase.java:97)
at android.test.ActivityInstrumentationTestCase2.getActivity(ActivityInstrumentationTestCase2.java:104)
at net.humblegames.bodylasticscalculator.test.MenuNavigationTestExperiment.setUp(MenuNavigationTestExperiment.java:42)
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 com.google.android.apps.common.testing.testrunner.GoogleInstrumentationTestRunner.onStart(GoogleInstrumentationTestRunner.java:167)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1701)
更新#1
我为该项目创建了一个干净的 eclipse 项目和一个干净的 Espresso 测试。我能够在 Espresso 测试运行期间重现相同的错误(但我仍然不确定此错误的原因是否与我的真实应用程序相同):
目标应用有 3 个 Activity (主要、第二、第三)。用户通过单击菜单项在它们之间导航:主 Activity 启动第二个,第二个 Activity 启动第三个。
我发现,如果我在第一次测试中调用 clickMenuItem()(见下文)和 Thread.sleep(500),这会导致第二次测试之前调用 getActivity() 中的 setUp() 发生崩溃。如果 sleep 超时小于 0.5 秒,则不会发生崩溃。
如果同时调用 Thread.sleep(10000) 而不调用 clickMenuItem() 也不会崩溃。第一次测试成功休眠10秒,第二次也能通过。
public class MainActivityTest extends
ActivityInstrumentationTestCase2<MainActivity> {
private String TAG = getClass().getSimpleName();
private Activity activity;
public MainActivityTest() {
super(MainActivity.class); }
public void setUp() throws Exception {
Log.d(TAG, "SETUP");
activity = getActivity();
super.setUp(); }
public void tearDown() throws Exception {
Log.d(TAG, "TEARDOWN");
super.tearDown(); }
public void testFirst() throws InterruptedException {
Log.d(TAG, "testFirst");
clickMenuItem("Start second activity");
clickMenuItem("Start third activity");
Thread.sleep(500);
}
public void testSecond() {
Log.d(TAG, "testSecond");
// this test will not run }
private void clickMenuItem(String menuItem) {
Log.d(TAG, "clickMenuItem");
openActionBarOverflowOrOptionsMenu(getInstrumentation()
.getTargetContext());
onView(withText(menuItem)).perform(click()); } }
主要 Activity :
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); }
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true; }
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.action_start_second_activity:
Intent intent = new Intent(this, SecondActivity.class);
startActivity(intent);
return true;
default:
return super.onOptionsItemSelected(item);
} } }
第二个 Activity :
public class SecondActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second); }
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.second, menu);
return true; }
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.action_start_third_activity:
Intent intent = new Intent(this, ThirdActivity.class);
startActivity(intent);
return true;
default:
return super.onOptionsItemSelected(item);
} } }
第三个 Activity :
public class ThirdActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_third); }
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.third, menu);
return true; }
}
最佳答案
我找到了解决方法。我在 tearDown() 方法中添加了多次点击后退按钮的代码,以关闭所有先前打开的 Activity 。不幸的是,我没有找到另一种方法来关闭 Espresso 中所有打开的 Activity 。 Robotium 有一个非常方便的方法用于此目的 solo.finishOpenedActivities()。
public void tearDown() throws Exception {
Log.d(TAG, "TEARDOWN");
goBackN();
super.tearDown();
}
private void goBackN() {
final int N = 10; // how many times to hit back button
try {
for (int i = 0; i < N; i++)
Espresso.pressBack();
} catch (com.google.android.apps.common.testing.ui.espresso.NoActivityResumedException e) {
Log.e(TAG, "Closed all activities", e);
}
}
关于android - getActivity() 调用导致 RuntimeException : Could not launch intent Intent act=android. intent.action.MAIN,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22592018/
运行真机调试时,出现如下问题。 Could not launch “MY-APP-NAME” iPhone has denied the launch request. Internal launch
每次单击“运行方式”按钮时,都会收到以下消息。 “选品无法推出,近期也没有推出。” 如果我选择“运行方式”按钮附近的下拉箭头,然后选择“新配置”选项(这是我设置为使用本地主机启动的配置)......它
启动过去有效的python文件时出现上述错误。我想我知道这是怎么发生的,但不确定如何解决。我有两台计算机双启动不同的操作系统,并使用保管箱保持它们之间的同步。过去,我以这种方式将我的Eclipse工作
我尝试使用 Eclipse 构建并运行“Hello world”,但它无法运行。我不使用 Android 或其他任何纯 C++。 http://i57.tinypic.com/x51h5h.png 最
这个问题在这里已经有了答案: "Selection cannot be launched and there are no recent launches” when Eclipse for Andr
在 Kotlin 中有多种启动协程的方法。我发现了几个示例,其中 GlobalScope 和 CoroutineScope 被使用。但是后者是在启动协程时直接创建的: 使用 GlobalScope :
我正在使用 10gen 版本在 OS X 上安装 MongoDB。 但是their installation tutorial事实证明对我来说有点稀缺。 到目前为止,我已经找到了安装为 launch
我在互联网上到处寻找,并尝试了论坛所说的一切,但没有任何效果。这个错误不断出现。我试过运行我的java项目(不适用于android),即使下拉运行按钮不起作用,因为它说“不适用”。 最佳答案 Ecli
我理解 std::async 对以下参数的作用。 std::launch::async std::launch::deferred 然而,std::launch::async | 会发生什么? std
我正在尝试使用 Eclipse 打开一个解压缩的 Android 项目,但每次我尝试运行或调试时都会显示此错误:“无法启动选择并且最近没有启动”。 所以我查找并尝试从现有源创建一个新项目,但打开时出现
我删除了 React Native 创建的默认启动屏幕文件,而是创建了一个启动图像。当我在模拟器中运行应用程序时,会显示启动图像,但是当我存档项目并通过 Testflight 打开它时,它仍然显示旧的
从深层链接(具有应用特定方案的 URL)启动我的 iOS 应用时,我在日志中收到此错误: lsd[738] : LaunchServices: application launch failed -
当我使用 visual studio code 调试 nodejs 应用程序时。visual studio code 告诉我 request 'launch': cannot launch targe
我创建了 Cocoa 应用程序,它是启动代理的类型。为了分发这个,我还创建了一个包,它安装该应用程序并将 launchagnet plist 文件复制到/Libraries/LaunchAgents
大家好,我今天在开发的 Java Swing 应用程序中发现了一个很大的疑问。 我发现的问题是launch() 当我在本地尝试我开发的应用程序时,它运行时没有任何延迟,但是当我使用 jnlp 从我的
我正在尝试使用 JavaFX 设置视频在 JFrame 中播放。 但是一旦我调用 launch() 函数来设置 JavaFX 并播放视频,我就会收到以下错误 应用程序构造函数中出现异常线程“AWT-E
我试图在 Android Studio 的外部工具中设置 javah 命令,但我得到了 IllegalArgumentException: Not a valid class name: Files\
我正在尝试创建一个简单的应用程序。我创建了一个新的 AVD。我启动了它。等了一个小时,模拟器只显示ANDROID。我卡住了。有人可以帮忙吗。 一个小时的样子请不要标记重复。我尽可能多地搜索和尝试。 我
进程文件: launch or launch.exe 进程名称: Vantarakis Launchh 进程类别:存在安全风险的进程 英文描述: launch.exe is a part o
将 google chrome 更新到最新版本 89.0.4389.82 (Official Build) (64-bit) 后开始出现错误。操作系统:Windows 10 错误: Puppeteer
我是一名优秀的程序员,十分优秀!