Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NoClassDefFoundError: java.util.Objects Android

Tags:

android

In two projects that I've contributed I've had this Error:

FATAL EXCEPTION: main
java.lang.NoClassDefFoundError: java.util.Objects

That because I've implemented hashCode and equals methods using Objects class.

@Override
public int hashCode() {
    int hash = 7;
    hash = 97 * hash + Objects.hashCode(this.image);
    hash = 97 * hash + Objects.hashCode(this.car);
    return hash;
}

@Override
public boolean equals(Object obj) {
    if (obj == null) {
        return false;
    }
    if (getClass() != obj.getClass()) {
        return false;
    }
    final SummaryContent other = (SummaryContent) obj;
    if (!Objects.equals(this.image, other.image)) {
        return false;
    }
    return Objects.equals(this.car, other.car);
}

When I compile I don't get error or warnings. Why might it be happening?

like image 489
rvillablanca Avatar asked Feb 19 '15 18:02

rvillablanca


2 Answers

I have the same problem in my new game. And i think, it happends, because different mobile manufacturer provide phones with different version of jvm.

My solution of this problem was copying implementation of methos equals from Objects to my project. It dirty, but work:

public static boolean equals(Object a, Object b) {
    return (a == b) || (a != null && a.equals(b));
}
like image 98
waxtah Avatar answered Sep 22 '22 15:09

waxtah


I was wrong. My device was android 4.1, which is api level 16 and the class Objects is since api level 19.

like image 29
rvillablanca Avatar answered Sep 22 '22 15:09

rvillablanca