Espresso:找不到与ID匹配的层次结构视图:android:id/home

Cyr*_*rus 6 android android-testing

当我运行我的测试用例时,我得到了例外.

android.support.test.espresso.NoMatchingViewException:找不到层次结构中的视图匹配:id:android:id/home

这是我的TestCase代码.

 public void nav_to_alarm_test(){
        onView(withId(R.id.navigation_notifications)).perform(click());
        onView(withId(R.id.rl_reminder)).perform(click());
        onView(withId(R.id.item_test)).perform(click());
        onView(withId(android.R.id.home)).perform(click());//throw exception here
        onView(withId(android.R.id.home)).perform(click());
    }
Run Code Online (Sandbox Code Playgroud)

这是android.R.id.home,属于ActionBar提供的按钮

 @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                finish();
                return true;
            case R.id.item_test:
                startActivityForResult(CreateOrUpdateReminderActivity.class,CMD_ADD_REMINDER);
                return true;
            default:
                return super.onOptionsItemSelected(item);

        }
    }
Run Code Online (Sandbox Code Playgroud)

为什么在资源ID android.R.id.home的层次结构中找不到视图?我有谷歌我的问题,但答案不是我想要的.
谢谢你的帮助.

Cyr*_*rus 4

与这里相同的问题(单击带有浓缩咖啡的主页图标
解决我的问题的是这一行

Espresso.pressBack();
Run Code Online (Sandbox Code Playgroud)