Fix some issues about AMS creation in tests.

In commit ce8db89ce529343262f7f4c669dd2d8976afcff6 we fixed a mem leak
issue caused by thread leak. That makes some tests using the constructor
in question crash. Therefore instead of trying to restore the original
state, convert these tests to use another constructor to avoid the mem
leak.

Also fixed another NPE when getting resources from null context and null
UiHandler for some of these tests.

Bug: 138840116
Test: atest FrameworksServicesTests:ActivityManagerServiceTest
Test: atest FrameworksServicesTests:ActivityManagerInternalTest
Test: atest FrameworksServicesTests:CoreSettingsObserverTest doesn't
fail for these reasons.
Test: atest FrameworksMockingServicesTests:AppCompactorTest
Test: atest FrameworksServicesTests:AppErrorDialogTest
Change-Id: Id349a3e1100faa5b4f5033d94c0f892d3f3745bf
diff --git a/services/tests/servicestests/src/com/android/server/am/AppErrorDialogTest.java b/services/tests/servicestests/src/com/android/server/am/AppErrorDialogTest.java
index 1dfce51..87cc0ff 100644
--- a/services/tests/servicestests/src/com/android/server/am/AppErrorDialogTest.java
+++ b/services/tests/servicestests/src/com/android/server/am/AppErrorDialogTest.java
@@ -29,6 +29,7 @@
 import com.android.server.wm.ActivityTaskManagerService;
 
 import org.junit.Before;
+import org.junit.Rule;
 import org.junit.Test;
 
 import java.io.File;
@@ -41,6 +42,9 @@
 @FlakyTest(bugId = 113616538)
 public class AppErrorDialogTest {
 
+    @Rule
+    public ServiceThreadRule mServiceThreadRule = new ServiceThreadRule();
+
     private Context mContext;
     private ActivityManagerService mService;
 
@@ -55,14 +59,19 @@
 
             @Override
             public Handler getUiHandler(ActivityManagerService service) {
-                return null;
+                return mServiceThreadRule.getThread().getThreadHandler();
             }
 
             @Override
             public boolean isNetworkRestrictedForUid(int uid) {
                 return false;
             }
-        });
+
+            @Override
+            public Context getContext() {
+                return mContext;
+            }
+        }, mServiceThreadRule.getThread());
         mService.mActivityTaskManager = new ActivityTaskManagerService(mContext);
         mService.mActivityTaskManager.initialize(null, null, mContext.getMainLooper());
     }