Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ExceptionInInitializerError in Android app?

In my Android app the WebView activity class has following line,

webView.addJavascriptInterface(new JSInterface(this), "Android");   

And in JSInterface class, I'm initializing Google "SpreadSheetService" like below,

import com.google.gdata.client.spreadsheet.SpreadsheetService;

--- some more imports ---


public class JSInterface {
    Context mContext;

    public SpreadsheetService service;

    /** Instantiate the interface and set the context */
    JSInterface(Context c) {
        mContext = c;
        service = new SpreadsheetService("List Demo");
    }

    ------- some more code -----

When I run the application I'm getting the following exception,

01-19 21:38:00.652: E/AndroidRuntime(4085): java.lang.ExceptionInInitializerError

which has the below trace

01-19 21:38:00.652: E/AndroidRuntime(4085): FATAL EXCEPTION: main
01-19 21:38:00.652: E/AndroidRuntime(4085): java.lang.ExceptionInInitializerError
01-19 21:38:00.652: E/AndroidRuntime(4085):     at com.android.quotes.JSInterface.<init>(JSInterface.java:33)
01-19 21:38:00.652: E/AndroidRuntime(4085):     at com.android.quotes.CHQuotesActivity.onCreate(CHQuotesActivity.java:19)
01-19 21:38:00.652: E/AndroidRuntime(4085):     at android.app.Activity.performCreate(Activity.java:4465)
01-19 21:38:00.652: E/AndroidRuntime(4085):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
01-19 21:38:00.652: E/AndroidRuntime(4085):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1919)
01-19 21:38:00.652: E/AndroidRuntime(4085):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
01-19 21:38:00.652: E/AndroidRuntime(4085):     at android.app.ActivityThread.access$600(ActivityThread.java:122)
01-19 21:38:00.652: E/AndroidRuntime(4085):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
01-19 21:38:00.652: E/AndroidRuntime(4085):     at android.os.Handler.dispatchMessage(Handler.java:99)
01-19 21:38:00.652: E/AndroidRuntime(4085):     at android.os.Looper.loop(Looper.java:137)
01-19 21:38:00.652: E/AndroidRuntime(4085):     at android.app.ActivityThread.main(ActivityThread.java:4340)
01-19 21:38:00.652: E/AndroidRuntime(4085):     at java.lang.reflect.Method.invokeNative(Native Method)
01-19 21:38:00.652: E/AndroidRuntime(4085):     at java.lang.reflect.Method.invoke(Method.java:511)

I searched Google, but didnt get any solution for this.. Any idea on why I'm getting this exception??

Peter

like image 959
Naveen Avatar asked Jan 19 '12 16:01

Naveen


3 Answers

Based on this documentation An ExceptionInInitializerError is thrown to indicate that an exception occurred during evaluation of a static initializer or the initializer for a static variable. Check your code has any static initialization logic.

like image 58
kosa Avatar answered Nov 08 '22 14:11

kosa


java.lang.ExceptionInInitializerError android OS 11

if this is related to OkHttp then update your version 4.4.0.

In this version its fixed.

implementation 'com.squareup.okhttp3:logging-interceptor:4.4.0'

thanks.

like image 35
Yogendra Avatar answered Nov 08 '22 14:11

Yogendra


If your android version is 11 or higher you might encounter java.lang.ExceptionInInitializerError and IllegalStateException for which you will need to add latest okhttp3 dependencies which are:

implementation 'com.squareup.okhttp3:okhttp:4.9.1'  

and

implementation 'com.squareup.okhttp3:logging-interceptor:4.9.0'
like image 33
SaminSyed Avatar answered Nov 08 '22 12:11

SaminSyed