微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

android – 无法在45秒内启动意图.也许主线程在合理的时间内没有闲置?

我正在写Espresso Test.这是测试用例

点击Fab,该应用程序启动QuizActivity.

让我向你解释我的应用程序.

所以应用程序的要求是 –

> assets文件夹中有JSON文件
>我必须解析它并将数据存储在数据库
>在主活动上将此数据从数据库加载到recyclerview.有Fab按钮,点击它应用程序将随机数据列表(我已经在recyclerview中加载)传递给QuizActivity

这是我编码的方式 –

>在MainActivity的onCreate()中,使用AsyncTask只将数据解析并插入数据库一次.
>数据可用后,通过AsyncTaskLoader将其加载到recyclerview中
>在Fab上设置Onclick监听器.并将所需数据传递给QuizActivity.

以下是我的测试

@RunWith(AndroidJUnit4.class)
public class ExampleInstrumentedTest {
   /* @Rule
    public IntentsTestRuletest(){
        onView(withId(R.id.fab)).perform(click());
        //Check if the text_question textview is displayed
        onView(withId(R.id.text_question)).check(matches(isdisplayed()));
    }

   /* @Test
    public void clickFloatingActionButton() {
        onView(withId(R.id.fab))
                .perform(click());
        intended(hasComponent(new ComponentName(getTargetContext(),QuizActivity.class)));
    }*/
}

我的方法是 –

>找到Fab
>执行点击
>检查是否显示text_question文本视图.请注意,此textview是关于QuizActivity的.

在我运行测试后,我得到了

java.lang.RuntimeException: Could not launch intent Intent {
act=android.intent.action.MAIN flg=0x14000000
cmp=my_package_name/.MainActivity } 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 1505287862579 and Now the
last time the queue went idle was: 1505287862579. If these numbers are
the same your activity might be hogging the event queue.

PS – 我关掉了所有的动画.我在代码中没有进度条.

还有一点需要注意,如果我在onCreate()方法中注释掉AsyncTask,AsyncTaskLoader,RecyclerView部分,那么测试就会通过.

我怀疑它可能是由于后台任务造成的.

有人遇到类似问题吗?如果您知道解决方案,请告诉我.我两天都在苦苦挣扎.我在stackoverflow上搜索了类似的线程,但没有任何对我有用.

最佳答案
我终于得到了解决方案!

实际上,罪魁祸首是我的适配器中的自定义视图.

Espresso等待AsyncTask或AsyncTaskLoader加载数据,但它看起来像自定义视图导致此问题,如异常中所述:

the main thread has not gone idle within a reasonable amount of time?
There Could be an animation or something constantly repainting the
screen.

无论如何要克服这个问题,我需要做的是 –

“如果测试正在运行,请将自定义视图的可见性设置为GONE”

你会怎么做?

 private AtomicBoolean isTestRunning;

    public synchronized boolean isTestRunning () {
        if (null == isTestRunning) {
            boolean istest;

            try {

                Class.forName ("Full path of your TestName"); // for e.g. com.example.MyTest

                istest = true;
            } catch (ClassNotFoundException e) {
                istest = false;
            }

            isTestRunning = new AtomicBoolean (istest);
        }

        return isRunningTest.get ();
    }

现在我在ViewHolder中使用Recyclerview,

public InsectHolder(View itemView)
        {
            super(itemView);
                       customView = (DangerLevelView) itemView.findViewById(R.id.danger_level_tv);

            if(isRunningtest()) {
                customView.setVisibility(View.GONE);
            }
        }

好的!你的考试将通过.

希望有人觉得它有用!

如果您对以下评论部分有任何疑问,请与我们联系!

原文地址:https://www.jb51.cc/android/430153.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐