I have a snippet of code creating a new String as follow:
private final static Charset UNICODE_CHARSET = Charset.forName("UTF-8");
public String makeNewUnicodeString(byte[] octects) {
return new String(octects, UNICODE_CHARSET);
}
It works fine when testing on my computer. But when I run it on Android emulator, it throws:
java.lang.NoSuchMethodError: java.lang.String.<init>
But this works:
public String makeUnicodeString(byte[] octets) {
try {
return new String(octets, "UTF-8")
} catch (UnsupportedEncodingException uee) {
// never throw.
}
}
I’m using Android 2.2 API 8, rev 2.
Since the constructor
String (byte[] data, Charset charset)was only added in API Level 9 (Android SDK 2.3). So updating SDK version solved my problems. Thanks everyone.Here the reference:
String – Android Developer Reference.
API Levels of Android Platform.