无法初始化 GeckoRuntime |壁虎查看 |安卓

Posted

技术标签:

【中文标题】无法初始化 GeckoRuntime |壁虎查看 |安卓【英文标题】:Failed to initialize GeckoRuntime | GeckoView | Android 【发布时间】:2020-04-09 05:23:32 【问题描述】:
 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 方法,但是当我单击返回时 重新打开应用程序,然后应用程序崩溃并显示 IllegalStateException “初始化 GeckoRuntime 失败。它第一次工作只有当我点击返回然后再次打开应用程序时才会崩溃”

日志如下

 Process: arholding.kargoshop.mk, PID: 16444
    java.lang.RuntimeException: Unable to start activity ComponentInfoarholding.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】:

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

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

更改您的代码

val runtime = GeckoRuntime.create(this)

val runtime = GeckoRuntime.getDefault(this)

解决方案 2: 退出应用时通过完成 Activity 终止进程,将此代码添加到您的 Activity 中。

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

【讨论】:

谢谢,我昨晚解决了,当时没有人回应。解决方案 1 对我有用

以上是关于无法初始化 GeckoRuntime |壁虎查看 |安卓的主要内容,如果未能解决你的问题,请参考以下文章

壁虎的最佳时间间隔,熊猫移位的问题

小壁虎(EFM32 Zero Gecko)开发板测量功耗

小壁虎(EFM32 Zero Gecko)开发板测量功耗

无法在无头模式下运行 Firefox

列表解析练习题

谁有不收费的手机图片恢复软件