title: NullPointerException when taking screenshot
body: <p>I'm trying to take screenshot, but I'm getting error</p>
  
  <pre><code>java.lang.NullPointerException: Attempt to invoke virtual method 'int android.graphics.Bitmap.getWidth()' on a null object reference
  </code></pre>
  
  <p>on line</p>
  
  <pre><code> Bitmap bitmap = Bitmap.createBitmap(view.getDrawingCache());
  </code></pre>
  
  <p>Main.java:</p>
  
  <pre><code>public class Main extends Activity {
  
       @Override
              protected void onCreate(Bundle savedInstanceState) {
                  super.onCreate(savedInstanceState);
                  setContentView(R.layout.main);
  
                  File file = new File(Environment.getExternalStoragePublicDirectory(
                          Environment.DIRECTORY_PICTURES), "Screenshots");
  
                  String date = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
  
                  try {
                      View view = getWindow().getDecorView().getRootView();
                      view.setDrawingCacheEnabled(true);
                      Bitmap bitmap = Bitmap.createBitmap(view.getDrawingCache());
                      view.setDrawingCacheEnabled(false);
  
                      File imgFile = new File(file.getPath() + File.separator +
                              "IMG_"+ date + ".jpg");
  
                      FileOutputStream outputStream = new FileOutputStream(imgFile);
                      bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream);
                      outputStream.flush();
                      outputStream.close();
                  } catch (Throwable e) {
                      e.printStackTrace();
                  }
              }
  }
  </code></pre>
  
  <p>main.xml:</p>
  
  <pre><code>&lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
      android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
      android:paddingRight="@dimen/activity_horizontal_margin"
      android:paddingTop="@dimen/activity_vertical_margin"
      android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".Main"&gt;
  
  &lt;/RelativeLayout&gt;
  </code></pre>
  
  <p>Why is this happening and how to fix this?</p>