初始化GeckoRuntime失败。 GeckoView |安卓系统

时间:2019-12-16 17:47:03

标签: android kotlin webview geckoview

 private fun setupGeckoView() {
        val runtime = GeckoRuntime.create(this) // crashes on this line
        geckoSession.open(runtime)
        geckoView.setSession(geckoSession)
        val url = String(Base64.decode(MYURL, Base64.DEFAULT))
        geckoSession.loadUri(url)
        geckoSession.progressDelegate = createProgressDelegate()
        geckoSession.settings.allowJavascript = true
    }
  

我在onCreat()中调用setUpGeckoView methon,但是当我单击返回并   重新打开应用程序,然后应用程序崩溃,并显示IllegalStateException   “无法初始化GeckoRuntime。它只有在我单击返回然后再次打开应用程序时才会崩溃,第一次起作用”。

下面给出了日志

 Process: arholding.kargoshop.mk, PID: 16444
    java.lang.RuntimeException: Unable to start activity ComponentInfo{arholding.kargoshop.mk/arholding.kargoshop.mk.SeckoActivity}: java.lang.IllegalStateException: Failed to initialize GeckoRuntime
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3447)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3594)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2146)
        at android.os.Handler.dispatchMessage(Handler.java:107)
        at android.os.Looper.loop(Looper.java:237)
        at android.app.ActivityThread.main(ActivityThread.java:7762)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1047)
     Caused by: java.lang.IllegalStateException: Failed to initialize GeckoRuntime
        at org.mozilla.geckoview.GeckoRuntime.create(GeckoRuntime.java:458)
        at org.mozilla.geckoview.GeckoRuntime.create(GeckoRuntime.java:333)
        at arholding.kargoshop.mk.SeckoActivity.setupGeckoView(SeckoActivity.kt:23)
        at arholding.kargoshop.mk.SeckoActivity.onCreate(SeckoActivity.kt:19)
        at android.app.Activity.performCreate(Activity.java:7981)
        at android.app.Activity.performCreate(Activity.java:7970)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1307)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3422)

1 个答案:

答案 0 :(得分:1)

如果已经有一个活动的Gecko实例正在运行,则将抛出此异常。有很多方法可以解决此问题。

解决方案1:获取给定上下文的默认运行时。

更改您的代码

val runtime = GeckoRuntime.create(this)

val runtime = GeckoRuntime.getDefault(this)

解决方案2:通过结束活动退出应用程序,然后将此代码添加到您的活动中。

override fun onDestroy() {
    Process.killProcess(Process.myPid())
    super.onDestroy()
}

解决方案3:仅在没有活动实例运行时创建新实例

private fun setupGeckoView() {
    if (geckoRuntime == null) {
        geckoRuntime = GeckoRuntime.create(this)
    }

    geckoSession.open(geckoRuntime!!)
    geckoView.setSession(geckoSession)
    val url = String(Base64.decode(MYURL, Base64.DEFAULT))
    geckoSession.loadUri(url)
    geckoSession.progressDelegate = createProgressDelegate()
    geckoSession.settings.allowJavascript = true
}

companion object {
    var geckoRuntime: GeckoRuntime? = null
}