eclipse Google Espresso java.lang.RuntimeException:无法启动意图 Intent { act=android.intent.action.MAIN

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21406246/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 21:29:52  来源:igfitidea点击:

Google Espresso java.lang.RuntimeException: Could not launch intent Intent { act=android.intent.action.MAIN

androideclipseruntimeexceptionandroid-espresso

提问by user3241003

I am new to Espresso UI testing.

我是 Espresso UI 测试的新手。

I am getting this error while running tests (ADT Eclipse IDE ).

我在运行测试 (ADT Eclipse IDE ) 时收到此错误。

The app is already developed and there are lots of request going on while launching the app. it is not possible to rewrite the app. but i need to find the way to test this UI even if there is any delay in the loading of the components.

该应用程序已经开发完毕,并且在启动该应用程序时有很多请求正在进行。无法重写应用程序。但我需要找到测试这个 UI 的方法,即使组件加载有任何延迟。

        java.lang.RuntimeException: Could not launch intent Intent { act=android.intent.action.MAIN flg=0x14000000 cmp=com.xx.android/com.yy.core.android.map.MapActivity } 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 1390913271702 and and now the last time the queue went idle was: 1390913271767. 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 com.gulesider.android.test.UItest.setUp(UItest.java:25)
        at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:190)
        at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:175)
        at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:555)
        at com.google.android.apps.common.testing.testrunner.GoogleInstrumentationTestRunner.onStart(GoogleInstrumentationTestRunner.java:167)
        at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1799)
  1. I have one library project called “Core” - it will not generate any .apk
  2. Also i have one Android project called “AA” which will access “Core”. - This is AA.apk
  3. Now i have created a test project called “UItest”
  1. 我有一个名为“Core”的库项目——它不会生成任何 .apk
  2. 我还有一个名为“AA”的 Android 项目,它将访问“Core”。- 这是 AA.apk
  3. 现在我创建了一个名为“UItest”的测试项目

Manifest:

显现:

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.AA.android.test"
        android:versionCode="1"
        android:versionName="1.0" >
    <uses-sdk android:minSdkVersion="8" 
            android:targetSdkVersion="18" />
    <instrumentation
      android:name="com.google.android.apps.common.testing.testrunner.GoogleInstrumentationTestRunner"
      android:targetPackage="com.AA.android"/>
    <application
            android:allowBackup="true"
            android:icon="@drawable/ic_launcher"
            android:label="@string/app_name" >
    <activity
                android:name="com.core.android.map.MapActivity"
                android:label="@string/app_name" >
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
            <uses-library android:name="android.test.runner" />
        </application>
    </manifest>

My test:

我的测试:

    public class UItest extends ActivityInstrumentationTestCase2<MapActivity> {
        public UItest() {
            super(MapActivity.class);
        }

        @Override
        public void setUp() throws Exception {
            super.setUp();

            getActivity();

        }

        public void testSearchBox() {

            Espresso.onView(ViewMatchers.withId(R.id.menu_button_logo)).perform(ViewActions.click());

        }

    }

回答by testsingh

For Espresso Testing it is highly recommend that you turn off system animations on the virtual or physical device(s) used for testing. So you can follow the steps below to manually turn off the animations:

对于 Espresso 测试,强烈建议您关闭用于测试的虚拟或物理设备上的系统动画。因此,您可以按照以下步骤手动关闭动画:

Under: Settings->
Developer options-> Drawing

在:设置->
开发人员选项->绘图

  1. Window Animations scale to OFF
  2. Transition animation scale to OFF
  3. Animator duration scale to OFF
  1. 窗口动画缩放为关闭
  2. 将动画比例转换为 OFF
  3. Animator 持续时间缩放为 OFF

回答by Murat

If there is a progress bar running when you create the activity, you get an error like this. You should cause a stop for the progress bar in order to continue running the test.

如果在创建活动时有进度条在运行,则会收到类似这样的错误。您应该停止进度条以继续运行测试。

回答by dev5678

I experienced this error while running Espresso tests on 6.0 devices but not on 5.1.1 or 7.0 devices. I tracked the cause down to using android:fadeScrollbarswithin a style. Removing this item from my style resolved the issue.

我在 6.0 设备上运行 Espresso 测试时遇到了这个错误,但在 5.1.1 或 7.0 设备上没有。我将原因归结为android:fadeScrollbars在样式中使用。从我的风格中删除这个项目解决了这个问题。

回答by denys

Probably you have animation inside your activity, which blocks espresso execution. You have to disable it - see https://github.com/googlesamples/android-testing/tree/master/ui/espresso/BasicSample

可能您的 Activity 中有动画,这会阻止 espresso 的执行。您必须禁用它 - 请参阅 https://github.com/googlesamples/android-testing/tree/master/ui/espresso/BasicSample

回答by luckyhandler

In my case a custom view caused this behaviour. It contained a Scrollerwhich was constantly scrolling. Unfortunately, I didn't find a solution for this issue until now except disabling it for the tests...

在我的情况下,自定义视图导致了这种行为。它包含一个不断滚动的Scroller。不幸的是,除了在测试中禁用它之外,我直到现在才找到解决此问题的方法......

回答by ajitksharma

At the very first page you will be calling too many request which will be taking time more than 15 seconds, the first page should be very lightwieght. Just try by creating one new welcome page and then calling your original welcome page. Hope this work for you.

在第一页你会调用太多的请求,这将花费超过 15 秒的时间,第一页应该非常轻巧。只需尝试创建一个新的欢迎页面,然后调用您原来的欢迎页面即可。希望这对你有用。

回答by Ignacio Tomas Crespo

Well, in my case it was caused by a strange thing.

好吧,就我而言,这是由一件奇怪的事情引起的。

One of my UI tests opened the external intent "android.app.action.CONFIRM_DEVICE_CREDENTIAL" so I decided to stub it. From now on, the MAIN intent didnt launch again until I manually closed the screen oipened by "android.app.action.CONFIRM_DEVICE_CREDENTIAL" from the recent tasks.

我的一个 UI 测试打开了外部意图“android.app.action.CONFIRM_DEVICE_CREDENTIAL”,所以我决定存根它。从现在开始,直到我手动关闭最近任务中由“android.app.action.CONFIRM_DEVICE_CREDENTIAL”打开的屏幕后,MAIN intent 才会再次启动。

No idea why this happened, and have no time now for research. Maybe later I will update this thread.

不知道为什么会这样,现在也没有时间研究。也许稍后我会更新这个线程。

回答by Ricardo

I faced this error when I trying to test the opening of another activity when the user clicked on a given view. What I was doing wrong was not replacing:

当我尝试在用户单击给定视图时测试另一个活动的打开时,我遇到了这个错误。我做错的不是替换:

@Rule
public ActivityTestRule<MyActivity> myActivityActivityTestRule = new ActivityTestRule<>(MyActivity.class);

Per:

每:

@Rule
public IntentsTestRule<MyActivity> myActivityActivityTestRule =
            new IntentsTestRule<>(MyActivity.class);

回答by Jeffery Ma

I have stuck into this problem for several hours. Finally, I got the reason.

我已经陷入这个问题好几个小时了。终于,我找到了原因。

This works for me.

这对我有用。

Here are some different reasons, according to the phenomenon.

根据现象,这里有一些不同的原因。

  • Activity can't be launched
  • Activity launched, but UI perform actions not work
  • 活动无法启动
  • 活动已启动,但 UI 执行操作不起作用

The first scenario: activity can't be launched

第一种情况:activity无法启动

Because of your target Activity maybe already in the activity stack. Add a CLEARflag and NEW_TASKflag

因为您的目标活动可能已经在活动堆栈中。添加CLEAR标志和NEW_TASK标志

    @get:Rule
    val activityRule = ActivityTestRule<MainActivity>(MainActivity::class.java)

    private lateinit var launchedActivity: MainActivity

    @Before
    fun setUp() {
        val intent = Intent(Intent.ACTION_PICK)
        //this is the key part
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK)
        //this is the key part
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
        launchedActivity = activityRule.launchActivity(intent)
    }

The second scenario: activity launched but UI perform actions not work

第二种情况:活动启动但用户界面执行操作不起作用

In this scenario may be because of

在这种情况下可能是因为

  1. Your code is executing some animation in a long time
  2. Your code is doing a UI logic that you don't notice
    • (like Handler post event or runnable)
    • (register a listener in ViewTreeObserver like OnPreDrawListener and didn't unregister in a proper timing)
  1. 你的代码在很长一段时间内正在执行一些动画
  2. 您的代码正在执行您没有注意到的 UI 逻辑
    • 如 Handler post 事件或 runnable
    • 在 ViewTreeObserver 中注册一个监听器,如 OnPreDrawListener 并且没有在适当的时间取消注册

These actions may lead to UI thread busy, so the espresso can not do the test.

这些操作可能会导致 UI 线程繁忙,因此 espresso 无法进行测试