2016-06-09 66 views
1

我從LoginActivity開始,登錄後,我的應用程序轉到下一個帶有RecyclerView的Activity。我需要參考Recycler View。我正在這樣做,但得到null使用android espresso,如何訪問第二個活動的視圖?

RecyclerView recyclerView = (RecyclerView) loginActivity.getActivity().findViewById(R.id.messages_list); 

它是空的,因爲loginActivity不包含RecyclerView。所以,我的問題是,當我移動到第二個活動,我怎麼能告訴意大利特濃咖啡和新活動,以取代loginActivity,這樣我可以做到這一點

RecyclerView recyclerView = (RecyclerView) currentActivity.getActivity().findViewById(R.id.messages_list); 

如果這是不可能的,有什麼其他的方法是有?

回答

1
Activity currentActivity; 

public Activity getActivityInstance() { 

    getInstrumentation().runOnMainSync(new Runnable() { 
     public void run() { 
      Collection resumedActivities = ActivityLifecycleMonitorRegistry.getInstance().getActivitiesInStage(Stage.RESUMED); 
      if (resumedActivities.iterator().hasNext()) { 
       currentActivity = (Activity) resumedActivities.iterator().next(); 
      } 
     } 
    }); 

    return currentActivity; 
} 

RecyclerView recyclerView = (RecyclerView) getActivityInstance().findViewById(R.id.messages_list); 
相關問題