title: cannot seem to get bitmap from view in android
body: <p>My problem is similar to the one in <a href="https://stackoverflow.com/questions/2339429/android-view-getdrawingcache-returns-null-only-null">Android View.getDrawingCache returns null, only null</a>.</p>
  
  <p>My code used to work with the method</p>
  
  <pre><code>private Bitmap getBitmapFromView(View v) {
      v.setDrawingCacheEnabled(true);
      v.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),
          MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
      v.layout(0, 0, v.getMeasuredWidth(), v.getMeasuredHeight());
      v.buildDrawingCache(true);
      Bitmap b = Bitmap.createBitmap(v.getDrawingCache());
      v.setDrawingCacheEnabled(false); // clear drawing cache
      return b;
  }
  </code></pre>
  
  <p>But today I changed the xml layout file. Since the change I have been getting NullPointerException at the line <code>Bitmap b = Bitmap.createBitmap(v.getDrawingCache())</code>. So I tried changing the method <code>getBitmapFromView</code> to </p>
  
  <pre><code>public static Bitmap loadBitmapFromView(View v) {
      Bitmap b = Bitmap.createBitmap(v.getLayoutParams().width, v.getLayoutParams().height, Bitmap.Config.ARGB_8888);
      Canvas c = new Canvas(b);
      v.layout(0, 0, v.getLayoutParams().width, v.getLayoutParams().height);
      v.draw(c);
      return b;
  }
  </code></pre>
  
  <p>But that didn't help. I get the null in the very first line.</p>
  
  <p>The situation is this: I am trying to convert the view into a bitmap. But I am doing this without first displaying the view on screen. Again, this all used to work until I changed the content of the xml layout file. The change was not even that big. I just moved stuff around and removed -- yes removed -- some of the subviews.</p>
  
  <p>In any case, I know for certain there is no problem with my layout file because I can display it on screen if I want; which I have done as part of troubleshooting.</p>
  
  <p>Someone suggested using a handler to call <code>getDrawingCache()</code>. If that's the answer, how would I write that code?</p>
  
  <p><strong>UPDATE</strong></p>
  
  <pre><code>View view = activity.getLayoutInflater().inflate(R.layout.my_view, null, false);
      findViewsByIds(view);//where I inflate subviews and add content to them.
  </code></pre>