Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FragmentManager throws IllegalArgumentException in Kotlin

I have the following method in my 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()
    }
}

I have to say this is what the Java->Kotlin automagic translator did with my previous Java code (save for the ? at the end of the line where I find the PrefsFragment by tag), so it may not be entirely kosher. I'm learning Kotlin by doing.

Now, when I call executePendingTransactions(), my app crashes throwing java.lang.IllegalArgumentException with error message "Parameter specified as non-null is null: method kotlin.jvm.internal.Intrinsics.checkParameterIsNotNull, parameter savedInstanceState". Now, yes, savedInstanceState is in fact null, but what does it have to do with the call that apparently throws the exception? I made sure that no other values/variables are null, so I'd trust the error message... but why?

EDIT Here is the stack trace, and the source code that's giving me nightmares

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)
like image 747
Morpheu5 Avatar asked Dec 04 '22 23:12

Morpheu5


1 Answers

The exception isn't actually coming from that code. It is coming from your CalculatorScreen.PrefsFragment.onCreatePreferences() method. If you look there, it looks like --

override fun onCreatePreferences(savedInstanceState: Bundle, rootKey : String)

and it should be the following to allow savedInstanceState to be null --

override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey : String)

like image 174
iagreen Avatar answered Dec 11 '22 17:12

iagreen