Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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);

like image 961
János Avatar asked Jul 04 '16 10:07

János


2 Answers

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

like image 198
CommonsWare Avatar answered Nov 08 '22 22:11

CommonsWare


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);
like image 40
Vikrant Kashyap Avatar answered Nov 08 '22 20:11

Vikrant Kashyap