我的 Fragment 中有以下方法

override fun onActivityCreated(savedInstanceState: Bundle?) {
    super.onActivityCreated(savedInstanceState)

    val fm = childFragmentManager
    var prefsFragment: PrefsFragment? = fm.findFragmentByTag(tableName) as PrefsFragment?
    if (prefsFragment == null) {
        prefsFragment = PrefsFragment()
        val ft = fm.beginTransaction()
        ft.add(R.id.parameters_fragment_container, prefsFragment, tableName)
        ft.commit()
        fm.executePendingTransactions()
    }
}

我不得不说这是 Java->Kotlin 自动翻译器对我以前的 Java 代码所做的(除了我通过标签找到 ? 的行末尾的 PrefsFragment ),所以它可能不完全是 kosher 。我正在通过实践来学习 Kotlin。

现在,当我调用 executePendingTransactions() 时,我的应用程序崩溃并抛出 java.lang.IllegalArgumentException 并显示错误消息“指定为非空的参数为空:方法 kotlin.jvm.internal.Intrinsics.checkParameterIsNotNull,参数savedInstanceState”。现在,是的,savedInstanceState 实际上是 null,但它与显然抛出异常的调用有什么关系?我确保没有其他值/变量为空,所以我相信错误消息......但为什么呢?

编辑 这是堆栈跟踪,以及让我做噩梦的源代码

https://gist.github.com/Morpheu5/8ec7e2064f53679405769b43c64f2cb7
E/AndroidRuntime: FATAL EXCEPTION: main
                  Process: net.morpheu5.bleedingscorecalculator, PID: 2685
                  java.lang.IllegalArgumentException: Parameter specified as non-null is null: method kotlin.jvm.internal.Intrinsics.checkParameterIsNotNull, parameter savedInstanceState
                      at net.morpheu5.bleedingscorecalculator.CalculatorScreen$PrefsFragment.onCreatePreferences(CalculatorScreen.kt:0)
                      at android.support.v7.preference.PreferenceFragmentCompat.onCreate(PreferenceFragmentCompat.java:223)
                      at android.support.v4.app.Fragment.performCreate(Fragment.java:2075)
                      at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1060)
                      at android.support.v4.app.BackStackRecord.setLastIn(BackStackRecord.java:838)
                      at android.support.v4.app.BackStackRecord.calculateFragments(BackStackRecord.java:861)
                      at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:719)
                      at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1682)
                      at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:594)
                      at net.morpheu5.bleedingscorecalculator.CalculatorScreen.onActivityCreated(CalculatorScreen.kt:34)
                      at android.support.v4.app.Fragment.performActivityCreated(Fragment.java:2096)
                      at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1138)
                      at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1295)
                      at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:801)
                      at android.support.v4.app.FragmentManagerImpl.execSingleAction(FragmentManager.java:1643)
                      at android.support.v4.app.BackStackRecord.commitNowAllowingStateLoss(BackStackRecord.java:679)
                      at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:143)
                      at android.support.v4.view.ViewPager.populate(ViewPager.java:1272)
                      at android.support.v4.view.ViewPager.populate(ViewPager.java:1120)
                      at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1646)
                      at android.view.View.measure(View.java:19857)
                      at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6083)
                      at android.support.design.widget.CoordinatorLayout.onMeasureChild(CoordinatorLayout.java:703)
                      at android.support.design.widget.HeaderScrollingViewBehavior.onMeasureChild(HeaderScrollingViewBehavior.java:90)
                      at android.support.design.widget.AppBarLayout$ScrollingViewBehavior.onMeasureChild(AppBarLayout.java:1367)
                      at android.support.design.widget.CoordinatorLayout.onMeasure(CoordinatorLayout.java:768)
                      at android.view.View.measure(View.java:19857)
                      at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6083)
                      at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
                      at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:139)
                      at android.view.View.measure(View.java:19857)
                      at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6083)
                      at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1464)
                      at android.widget.LinearLayout.measureVertical(LinearLayout.java:758)
                      at android.widget.LinearLayout.onMeasure(LinearLayout.java:640)
                      at android.view.View.measure(View.java:19857)
                      at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6083)
                      at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
                      at android.view.View.measure(View.java:19857)
                      at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6083)
                      at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1464)
                      at android.widget.LinearLayout.measureVertical(LinearLayout.java:758)
                      at android.widget.LinearLayout.onMeasure(LinearLayout.java:640)
                      at android.view.View.measure(View.java:19857)
                      at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6083)
                      at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
                      at com.android.internal.policy.DecorView.onMeasure(DecorView.java:689)
                      at android.view.View.measure(View.java:19857)
                      at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2275)
E/AndroidRuntime:     at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1366)
                      at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1619)
                      at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1254)
                      at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6337)
                      at android.view.Choreographer$CallbackRecord.run(Choreographer.java:874)
                      at android.view.Choreographer.doCallbacks(Choreographer.java:686)
                      at android.view.Choreographer.doFrame(Choreographer.java:621)
                      at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:860)
                      at android.os.Handler.handleCallback(Handler.java:751)
                      at android.os.Handler.dispatchMessage(Handler.java:95)
                      at android.os.Looper.loop(Looper.java:154)
                      at android.app.ActivityThread.main(ActivityThread.java:6119)
                      at java.lang.reflect.Method.invoke(Native Method)
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)

最佳答案

异常实际上并非来自该代码。它来自您的 CalculatorScreen.PrefsFragment.onCreatePreferences() 方法。如果你看那里,它看起来像——
override fun onCreatePreferences(savedInstanceState: Bundle, rootKey : String)
它应该是以下内容以允许 savedInstanceStatenull --
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey : String)

关于android - FragmentManager 在 Kotlin 中抛出 IllegalArgumentException,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/41146221/

10-10 07:09