9

在我的 Android 应用程序中,WebView 活动类具有以下行,

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

在 JSInterface 类中,我正在初始化 Google“SpreadSheetService”,如下所示,

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

当我运行应用程序时,出现以下异常,

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

有以下痕迹

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)

我搜索了谷歌,但没有得到任何解决方案。关于为什么我会得到这个异常的任何想法?

彼得

4

5 回答 5

16

根据此文档,抛出 ExceptionInInitializerError 以指示在评估静态初始化程序或静态变量的初始化程序期间发生异常。检查您的代码是否有任何静态初始化逻辑。

于 2012-01-19T16:30:25.037 回答
1

如果您的 android 版本是 11 或更高版本,您可能会遇到java.lang.ExceptionInInitializerError并且IllegalStateException您需要添加最新的 okhttp3 依赖项,这些依赖项是:

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

implementation 'com.squareup.okhttp3:logging-interceptor:4.9.0'
于 2021-11-04T10:37:19.770 回答
1

java.lang.ExceptionInInitializerError 安卓操作系统 11

如果这与 OkHttp 相关,则更新您的版本 4.4.0。

在这个版本中它是固定的。

实现 'com.squareup.okhttp3:logging-interceptor:4.4.0'

谢谢。

于 2020-10-02T05:50:49.797 回答
0

ExceptionInInitializerError更新的链接)显示在静态初始化程序unexpected exception中发生了一个。从字面上看,如果显示了这一点,则应该理解 Java 未能对静态初始化程序块静态变量的实例化进行评估。exception

无论如何,静态初始化程序遇到的每一刻都会exception自动包装到ExceptionInInitializerError类的实例中。以这种方式,它保留了对实际的引用exception作为根本原因。

在我的情况下,使用以下异常跟踪:

   java.lang.ExceptionInInitializerError
        at com.fole_Studios.sup.database.DBqueries.getAllUniversities(DBqueries.java:581)
        at com.fole_Studios.sup.RegisterFragment.universityDataList(RegisterFragment.java:216)
         ...............
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.google.firebase.auth.FirebaseUser.getUid()' on a null object reference
        at com.fole_Studios.sup.database.DBqueries.<clinit>(DBqueries.java:80)

我直接参考了导致错误的原因Caused by:。因此,我发现我正在实例化一个具有值的静态变量null。我解决了它,一切都很完美。

于 2021-01-21T00:38:30.483 回答
0

奇怪的!但是让我分享一下我的经验,也许它可以帮助某人!

正在开发以前运行没有任何问题的 Android 应用程序。在我休息我的 Android 设备并在重置后第一次安装应用程序之后。我得到了这个例外java.lang.ExceptionInInitializerError

  1. 我卸载了应用程序
  2. 清理项目
  3. 重建项目

之后,当我运行应用程序时,异常消失了。

我不知道发生了什么,但上述步骤刚刚解决了这个问题。

于 2020-12-29T18:45:59.237 回答