Fix NPE in TvInputService and TvView

Bug: 14781233, Bug: 14629185
Change-Id: I8511674f4f3864b3deaa58f4c95b9c91e68f49f4
diff --git a/core/java/android/tv/TvInputService.java b/core/java/android/tv/TvInputService.java
index 1d6298d..eeb738d 100644
--- a/core/java/android/tv/TvInputService.java
+++ b/core/java/android/tv/TvInputService.java
@@ -489,7 +489,7 @@
                     }
                 }
             }
-            if (mOverlayView == null) {
+            if (mOverlayView == null || !mOverlayView.isAttachedToWindow()) {
                 return Session.DISPATCH_NOT_HANDLED;
             }
             if (!mOverlayView.hasWindowFocus()) {
diff --git a/core/java/android/tv/TvView.java b/core/java/android/tv/TvView.java
index 80501e8..7eaa86b 100644
--- a/core/java/android/tv/TvView.java
+++ b/core/java/android/tv/TvView.java
@@ -347,7 +347,9 @@
         public void onSessionCreated(Session session) {
             if (this != mSessionCallback) {
                 // This callback is obsolete.
-                session.release();
+                if (session != null) {
+                    session.release();
+                }
                 return;
             }
             mSession = session;