Fixing NPE in raw contact sorting

Bug: 2513827
Change-Id: I92e263d8d258ab49d4b513e8acd0586354af7c9d
diff --git a/src/com/android/contacts/ui/EditContactActivity.java b/src/com/android/contacts/ui/EditContactActivity.java
index 35224d2..b79ba0f 100644
--- a/src/com/android/contacts/ui/EditContactActivity.java
+++ b/src/com/android/contacts/ui/EditContactActivity.java
@@ -242,7 +242,7 @@
             final boolean hasState = entitySet.size() > 0;
             if (hasExtras && hasState) {
                 // Find source defining the first RawContact found
-                final EntityDelta state = entitySet.get(0);
+                final EntityDelta state = target.mState.get(0);
                 final String accountType = state.getValues().getAsString(RawContacts.ACCOUNT_TYPE);
                 final ContactsSource source = sources.getInflatedSource(accountType,
                         ContactsSource.LEVEL_CONSTRAINTS);
@@ -1350,7 +1350,7 @@
             return -1;
         } else if (twoIsGoogle && !oneIsGoogle) {
             return 1;
-        } else {
+        } else if (oneIsGoogle && twoIsGoogle){
             skipAccountTypeCheck = true;
         }
 
@@ -1375,8 +1375,14 @@
         }
 
         // Both are in the same account, fall back to contact ID
-        long oneId = oneValues.getAsLong(RawContacts._ID);
-        long twoId = twoValues.getAsLong(RawContacts._ID);
+        Long oneId = oneValues.getAsLong(RawContacts._ID);
+        Long twoId = twoValues.getAsLong(RawContacts._ID);
+        if (oneId == null) {
+            return -1;
+        } else if (twoId == null) {
+            return 1;
+        }
+
         return (int)(oneId - twoId);
     }