title: How to get a layout from activity that is linked with onClick attribute in XML?
body: <p>I'm a beginner in Android and I'm testing my code using Android <code>JUnit</code> test.</p>
  
  <p>So, I have a test activity that extends <code>ActivityInstrumentationTestCase2&lt;Activity&gt;</code>.</p>
  
  <p>Activity has it's own layout in <code>onCreate()</code> method. (Main layout)</p>
  
  <p>In my XML, I have a <code>onClick</code> attribute for a button that calls <code>foo()</code> method.</p>
  
  <p>Back to Activity, in the <code>foo(View v)</code>, I set my content view to a different layout.</p>
  
  <p>I want to test that layout.</p>
  
  <p>How do I get the layout though??</p>
  
  <p>I know for the main layout, I can do this.</p>
  
  <pre><code>Activity act = getActivity();
  View mainLayout = (View) act.findViewById(bla.bla.bla.R.id.main_layout);
  </code></pre>
  
  <p>How do I get the layout that I set in <code>foo(View v)</code>??</p>
  
  <p>I've already tried doing,</p>
  
  <pre><code>fooLayout = (View) act.findViewById(bla.bla.bla.R.id.foo_layout);
  </code></pre>
  
  <p>and</p>
  
  <pre><code>act.setContentView(bla.bla.bla.R.layout.foo_layout);
  fooLayout = (View) act.findViewById(bla.bla.bla.R.id.foo_layout);
  </code></pre>
  
  <p>I think I got <code>NullPointerException</code> for the first one and <code>android.view.ViewRootImpl$CalledFromWrongThreadException</code>
  for the second one.</p>