János
János

Reputation: 35090

java.lang.NoClassDefFoundError: java.nio.charset.StandardCharsets

In simulator I can run my app, but on device with Jelly Bean OS I get a crash. Any idea why?

07-04 12:51:57.576 18243-18279/com.j4nos.moviebuffs6 E/AndroidRuntime: FATAL EXCEPTION: Thread-7940
          java.lang.NoClassDefFoundError: java.nio.charset.StandardCharsets
          at com.j4nos.moviebuffs6.Utility$1.run(Utility.java:52)
          at java.lang.Thread.run(Thread.java:838)

This is the line I need character encoding:

byte[] out = str.getBytes(StandardCharsets.UTF_8);

Upvotes: 12

Views: 9017

Answers (4)

CAMD_3441
CAMD_3441

Reputation: 3154

As many mentioned, you can just use "UTF-8" in place of StandardCharsets.UTF_8

If you right click and 'go to source' on that variable you'll see this, where you see that the variable UTF_8 is tied to the string value "UTF_8"

You can see the other charsets string variables as well.

enter image description here

Upvotes: 0

Vikrant Kashyap
Vikrant Kashyap

Reputation: 6856

There are various overloaded .getBytes() method is declared in String Class.

  public void getBytes(int, int, byte[], int);
  public byte[] getBytes(java.lang.String) throws java.io.UnsupportedEncodingException;
  public byte[] getBytes(java.nio.charset.Charset);
  public byte[] getBytes();

You can use any of it. but you should try this..

 byte[] out = str.getBytes("UTF-8");

instead of

byte[] out = str.getBytes(StandardCharsets.UTF_8);

Upvotes: 4

Fildor
Fildor

Reputation: 16129

The question is: Do you want to support Jelly Bean?

If yes: You can instead use the String-Parameterized version of the getBytes Method. You could even split codelines to use one version over the other depended on API Version running. (I do not recommend this!)

If no: Just restrict your app to a minimum API-Version equal to or higher than 19.

Upvotes: 0

CommonsWare
CommonsWare

Reputation: 1007296

StandardCharsets was added in API Level 19. It is not available for any of the Jelly Bean versions of Android.

Upvotes: 25

Related Questions