Fix NPE in accountPrefController

Change-Id: I83dba35604c501780fa9a99d122b9d9bc8068661
Fix: 34193417
Test: RunSettingsRoboTests
diff --git a/src/com/android/settings/accounts/AccountPreferenceController.java b/src/com/android/settings/accounts/AccountPreferenceController.java
index ad3fac1..0ddd366 100644
--- a/src/com/android/settings/accounts/AccountPreferenceController.java
+++ b/src/com/android/settings/accounts/AccountPreferenceController.java
@@ -311,7 +311,10 @@
             preferenceGroup.setContentDescription(
                 mContext.getString(R.string.accessibility_category_personal));
         }
-        mParent.getPreferenceScreen().addPreference(preferenceGroup);
+        final PreferenceScreen screen = mParent.getPreferenceScreen();
+        if (screen != null) {
+            screen.addPreference(preferenceGroup);
+        }
         profileData.preferenceGroup = preferenceGroup;
         if (userInfo.isEnabled()) {
             profileData.authenticatorHelper = new AuthenticatorHelper(context,
@@ -367,6 +370,9 @@
 
     void cleanUpPreferences() {
         PreferenceScreen screen = mParent.getPreferenceScreen();
+        if (screen == null) {
+            return;
+        }
         for (int i = 0; i < mProfiles.size(); i++) {
             final PreferenceGroup preferenceGroup = mProfiles.valueAt(i).preferenceGroup;
             screen.removePreference(preferenceGroup);
diff --git a/tests/robotests/src/com/android/settings/accounts/AccountPreferenceControllerTest.java b/tests/robotests/src/com/android/settings/accounts/AccountPreferenceControllerTest.java
index 552c689..398e9c5 100644
--- a/tests/robotests/src/com/android/settings/accounts/AccountPreferenceControllerTest.java
+++ b/tests/robotests/src/com/android/settings/accounts/AccountPreferenceControllerTest.java
@@ -190,6 +190,24 @@
     }
 
     @Test
+    @Config(shadows = {ShadowAccountManager.class, ShadowContentResolver.class})
+    public void onResume_noPreferenceScreen_shouldNotCrash() {
+        final List<UserInfo> infos = new ArrayList<>();
+        infos.add(new UserInfo(1, "user 1", 0));
+        when(mUserManager.isManagedProfile()).thenReturn(false);
+        when(mUserManager.isLinkedUser()).thenReturn(false);
+        when(mUserManager.getProfiles(anyInt())).thenReturn(infos);
+
+        AccessiblePreferenceCategory preferenceGroup = mock(AccessiblePreferenceCategory.class);
+        when(mAccountHelper.createAccessiblePreferenceCategory(any(Context.class))).thenReturn(
+                preferenceGroup);
+
+        mController.onResume();
+
+        // Should not crash
+    }
+
+    @Test
     public void updateRawDataToIndex_ManagedProfile_shouldNotUpdate() {
         final List<SearchIndexableRaw> data = new ArrayList<>();
         when(mUserManager.isManagedProfile()).thenReturn(true);