Fix monkey NPE

Bug 33177567

Monkey was causing a commit() while the activity was shutting down.
This protects from an NPE in this unlikely situation.

Test: cts-tradefed run singleCommand cts -d --skip-preconditions
      --skip-connectivity-check -m CtsFragmentTestCases

Change-Id: I832cfa8fb187c61b4d2f5a8a4d0f508d7a435171
(cherry picked from commit 3005e17b03d60a4ce0ed5ba04a9f474533c5c564)
diff --git a/core/java/android/app/BackStackRecord.java b/core/java/android/app/BackStackRecord.java
index abb098f..84adbe5 100644
--- a/core/java/android/app/BackStackRecord.java
+++ b/core/java/android/app/BackStackRecord.java
@@ -16,6 +16,8 @@
 
 package android.app;
 
+import android.content.Context;
+import android.content.pm.ApplicationInfo;
 import android.os.Build;
 import android.os.Parcel;
 import android.os.Parcelable;
@@ -349,9 +351,17 @@
 
     public BackStackRecord(FragmentManagerImpl manager) {
         mManager = manager;
-        int targetSdkVersion = manager.mHost.getContext().getApplicationInfo().targetSdkVersion;
-        // TODO: make the check N_MR1 or O
-        mAllowOptimization = targetSdkVersion > Build.VERSION_CODES.N;
+        FragmentHostCallback host = manager.mHost;
+        if (host != null) {
+            Context context = host.getContext();
+            if (context != null) {
+                ApplicationInfo info = context.getApplicationInfo();
+                if (info != null) {
+                    int targetSdkVersion = info.targetSdkVersion;
+                    mAllowOptimization = targetSdkVersion > Build.VERSION_CODES.N_MR1;
+                }
+            }
+        }
     }
 
     public int getId() {