2017-05-10 54 views
6

當我運行我的測試用例時,我得到了異常。Espresso:層次結構中沒有視圖與id匹配:android:id/home

android.support.test.espresso.NoMatchingViewException:在體系中沒有的觀點找到匹配:id爲:機器人:ID /家

這是我的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()); 
    } 

這是 android.R.id.home,屬於由動作條

提供的按鈕
@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); 

     } 
    } 

爲什麼找不到層次視圖資源ID android.R.id.home? 我有谷歌我的問題,但答案不是我想要的。
感謝您的幫助。喜歡這裏

+2

不[這](https://stackoverflow.com/a/35462828/1083957)幫你嗎? – azizbekian

+0

@azizbekian我在他的答案中找到了解決方案,謝謝! – Cyrus

回答