Fix NPE in TextureView

Bug: 30779663
Change-Id: Ib3d40b60674ab3de8bfcfdd8489b89cf07b8d8b3
diff --git a/core/java/android/view/TextureView.java b/core/java/android/view/TextureView.java
index a42da85..a5900e6 100644
--- a/core/java/android/view/TextureView.java
+++ b/core/java/android/view/TextureView.java
@@ -229,7 +229,6 @@
     @Override
     protected void destroyHardwareResources() {
         destroyHardwareLayer();
-        mUpdateSurface = mSurface != null;
     }
 
     private void destroyHardwareLayer() {
@@ -376,17 +375,17 @@
             }
 
             mLayer = mAttachInfo.mHardwareRenderer.createTextureLayer();
-            if (!mUpdateSurface) {
+            boolean createNewSurface = (mSurface == null);
+            if (createNewSurface) {
                 // Create a new SurfaceTexture for the layer.
                 mSurface = new SurfaceTexture(false);
                 mLayer.setSurfaceTexture(mSurface);
                 nCreateNativeWindow(mSurface);
             }
             mSurface.setDefaultBufferSize(getWidth(), getHeight());
-
             mSurface.setOnFrameAvailableListener(mUpdateListener, mAttachInfo.mHandler);
 
-            if (mListener != null && !mUpdateSurface) {
+            if (mListener != null && createNewSurface) {
                 mListener.onSurfaceTextureAvailable(mSurface, getWidth(), getHeight());
             }
             mLayer.setLayerPaint(mLayerPaint);
@@ -745,9 +744,11 @@
                     "released SurfaceTexture");
         }
         if (mSurface != null) {
+            nDestroyNativeWindow();
             mSurface.release();
         }
         mSurface = surfaceTexture;
+        nCreateNativeWindow(mSurface);
 
         /*
          * If the view is visible and we already made a layer, update the