Monkey proofing

 Bug: 5531453

Change-Id: I0a8158212d8b2de638bae3c66bb2b9aa3bf85c1d
diff --git a/src/com/android/browser/PhoneUi.java b/src/com/android/browser/PhoneUi.java
index ffcaaf8..a13eca3 100644
--- a/src/com/android/browser/PhoneUi.java
+++ b/src/com/android/browser/PhoneUi.java
@@ -529,12 +529,14 @@
                 if (mTitleBarBitmap == null
                         || mTitleBarBitmap.getWidth() != tbar.getWidth()
                         || mTitleBarBitmap.getHeight() != tbar.getEmbeddedHeight()) {
-                    mTitleBarBitmap = Bitmap.createBitmap(tbar.getWidth(),
-                            tbar.getEmbeddedHeight(), Bitmap.Config.RGB_565);
+                    mTitleBarBitmap = safeCreateBitmap(tbar.getWidth(),
+                            tbar.getEmbeddedHeight());
                 }
-                Canvas c = new Canvas(mTitleBarBitmap);
-                tbar.draw(c);
-                c.setBitmap(null);
+                if (mTitleBarBitmap != null) {
+                    Canvas c = new Canvas(mTitleBarBitmap);
+                    tbar.draw(c);
+                    c.setBitmap(null);
+                }
             } else {
                 mTitleBarBitmap = null;
             }
@@ -544,18 +546,28 @@
             if (mContentBitmap == null
                     || mContentBitmap.getWidth() != web.getWidth()
                     || mContentBitmap.getHeight() != h) {
-                mContentBitmap = Bitmap.createBitmap(web.getWidth(), h,
-                        Bitmap.Config.RGB_565);
+                mContentBitmap = safeCreateBitmap(web.getWidth(), h);
             }
-            Canvas c = new Canvas(mContentBitmap);
-            int tx = web.getScrollX();
-            int ty = web.getScrollY();
-            c.translate(-tx, -ty - tbar.getEmbeddedHeight());
-            web.draw(c);
-            c.setBitmap(null);
+            if (mContentBitmap != null) {
+                Canvas c = new Canvas(mContentBitmap);
+                int tx = web.getScrollX();
+                int ty = web.getScrollY();
+                c.translate(-tx, -ty - tbar.getEmbeddedHeight());
+                web.draw(c);
+                c.setBitmap(null);
+            }
             mContent.setImageBitmap(mContentBitmap);
         }
 
+        private Bitmap safeCreateBitmap(int width, int height) {
+            if (width <= 0 || height <= 0) {
+                Log.w(LOGTAG, "safeCreateBitmap failed! width: " + width
+                        + ", height: " + height);
+                return null;
+            }
+            return Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
+        }
+
         public void set(Bitmap image) {
             mTitle.setVisibility(View.GONE);
             mContent.setImageBitmap(image);