gpt4 book ai didi

android - 添加到应用程序中不存在请求的 flutter 引擎

转载 作者:行者123 更新时间:2023-12-04 13:32:54 30 4
gpt4 key购买 nike

我已经使用缓存的 flutter 引擎 flutter 到现有的 native 应用程序(添加到应用程序)中。

override fun onCreate(savedInstanceState: Bundle?) {
Log.d(TAG, "onCreate")


super.onCreate(savedInstanceState)

setContentView(R.layout.activity_main)
context = this.application.applicationContext
if (!FlutterEngineCache.getInstance().contains(
FLUTTER_ENGINE
)
) {
mFlutterEngine = FlutterEngine(context)
mFlutterEngine.dartExecutor
.executeDartEntrypoint(DartExecutor.DartEntrypoint.createDefault())
FlutterEngineCache.getInstance()
.put(FLUTTER_ENGINE, mFlutterEngine)
}
//Create Flutter Fragment
mFragmentManager = supportFragmentManager
mFlutterFragment = mFragmentManager.findFragmentByTag(FLUTTER_FRAGMENT) as FlutterFragment?
if (mFlutterFragment == null) {
mFlutterFragment =
FlutterFragment.withCachedEngine(FLUTTER_ENGINE).transparencyMode(FlutterView.TransparencyMode.opaque).build()
mFragmentManager
.beginTransaction()
.add(R.id.fragment_container, mFlutterFragment as Fragment, FLUTTER_FRAGMENT)
.commit()
} else {
mFragmentManager
.beginTransaction()
.show(mFragmentManager.findFragmentByTag(FLUTTER_FRAGMENT)!!)
.commit()
}
}
我在生产应用程序中遇到以下崩溃
  Caused by: java.lang.IllegalStateException: The requested cached FlutterEngine did not exist in the FlutterEngineCache: 'FLUTTER_ENGINE'
at io.flutter.embedding.android.FlutterActivityAndFragmentDelegate.setupFlutterEngine(FlutterActivityAndFragmentDelegate.java:211)
at io.flutter.embedding.android.FlutterActivityAndFragmentDelegate.onAttach(FlutterActivityAndFragmentDelegate.java:157)
at io.flutter.embedding.android.FlutterFragment.onAttach(FlutterFragment.java:597)
at androidx.fragment.app.Fragment.performAttach(Fragment.java:2574)
at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManagerImpl.java:828)
at androidx.fragment.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManagerImpl.java:1238)
at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManagerImpl.java:1303)
at androidx.fragment.app.FragmentManagerImpl.dispatchStateChange(FragmentManagerImpl.java:2659)
at androidx.fragment.app.FragmentManagerImpl.dispatchCreate(FragmentManagerImpl.java:2607)
at androidx.fragment.app.FragmentController.dispatchCreate(FragmentController.java:235)
at androidx.fragment.app.FragmentActivity.onCreate(FragmentActivity.java:316)
at androidx.appcompat.app.AppCompatActivity.onCreate(AppCompatActivity.java:106)
at com.flutter.android.MainActivity.onCreate(MainActivity.kt:33)

最佳答案

How to reproduce


  • 打开开发者设置,将“后台进程”设置为“否”
    后台进程”并设置为不保留 Activity 。
  • 打开应用程序新闻和主页
  • 再次打开应用程序..它会崩溃并出现异常。
    原因:java.lang.IllegalStateException:请求的缓存 FlutterEngine 在 FlutterEngineCache 中不存在:'FLUTTER_ENGINE'

  • Solution


    super.oncreate() 之前初始化 flutter 引擎
    override fun onCreate(savedInstanceState: Bundle?) {
    Log.d(TAG, "onCreate")
    context = this.application.applicationContext
    if (!FlutterEngineCache.getInstance().contains(
    FLUTTER_ENGINE
    )
    ) {
    mFlutterEngine = FlutterEngine(context)
    mFlutterEngine.dartExecutor
    .executeDartEntrypoint(DartExecutor.DartEntrypoint.createDefault())
    FlutterEngineCache.getInstance()
    .put(FLUTTER_ENGINE, mFlutterEngine)
    }


    super.onCreate(savedInstanceState)

    setContentView(R.layout.activity_main)
    //Create Flutter Fragment
    mFragmentManager = supportFragmentManager
    mFlutterFragment = mFragmentManager.findFragmentByTag(FLUTTER_FRAGMENT) as FlutterFragment?
    if (mFlutterFragment == null) {
    mFlutterFragment =
    FlutterFragment.withCachedEngine(FLUTTER_ENGINE).transparencyMode(FlutterView.TransparencyMode.opaque).build()
    mFragmentManager
    .beginTransaction()
    .add(R.id.fragment_container, mFlutterFragment as Fragment, FLUTTER_FRAGMENT)
    .commit()
    } else {
    mFragmentManager
    .beginTransaction()
    .show(mFragmentManager.findFragmentByTag(FLUTTER_FRAGMENT)!!)
    .commit()
    }

    关于android - 添加到应用程序中不存在请求的 flutter 引擎,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64010393/

    30 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com