OSDN Git Service

Avoid IllegalArgumentException in Preference.java compareTo()
authorCarsten Hauge <carsten.hauge@sonymobile.com>
Mon, 4 Feb 2013 13:45:20 +0000 (14:45 +0100)
committerHenrik Baard <henrik.baard@sonymobile.com>
Mon, 29 Apr 2013 09:17:54 +0000 (11:17 +0200)
In some situations the collection.sort() algoritm fails in compareTo()
with a: java.lang.IllegalArgumentException: Comparison method violates
its general contract!, due to a more strict validation of the compare
contract.
This strict validation was introduced in java 1.7.
See http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6804124
This fix should prevent the sorting from crashing.

Change-Id: I64230e7f62f83c99d7a6274964bb2988ae843826

core/java/android/preference/Preference.java

index e343e83..6c02965 100644 (file)
@@ -1072,6 +1072,9 @@ public class Preference implements Comparable<Preference>, OnDependencyChangeLis
                 || (mOrder == DEFAULT_ORDER && another.mOrder != DEFAULT_ORDER)) {
             // Do order comparison
             return mOrder - another.mOrder; 
+        } else if (mTitle == another.mTitle) {
+            // If titles are null or share same object comparison
+            return 0;
         } else if (mTitle == null) {
             return 1;
         } else if (another.mTitle == null) {