2016-12-14 67 views
1

我在下面的方法我FragmentFragmentManager拋出:IllegalArgumentException - 在科特林

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的>科特林AUTOMAGIC翻譯與我以前的Java代碼(保存爲?在做行尾的地方,我通過標籤找到PrefsFragment),所以它可能不完全是猶太教。我正在學習Kotlin。

現在,當我打電話executePendingTransactions(),我的應用程序崩潰,錯誤消息拋出java.lang.IllegalArgumentException「指定非空參數爲空:方法kotlin.jvm.internal.Intrinsics.checkParameterIsNotNull,參數savedInstanceState」。現在,是的,savedInstanceState實際上是空的,但它與顯然拋出異常的調用有什麼關係?我確信沒有其他值/變量是空的,所以我相信錯誤信息......但爲什麼?

編輯這裏是堆棧跟蹤,而這給我的源代碼的噩夢

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) 
+1

那麼,如果所有'檢查裏面'PrefsFragment' savedInstanceState的類型是'Bundle?'。 –

+0

是的,他們是,每當我訪問它們時,我都會防止它被「空」,除了當我將它傳遞給超類。 – Morpheu5

+1

請告訴我們錯誤stacktrace。 – miensol

回答

7

異常實際上並沒有從該代碼的到來。它來自您的CalculatorScreen.PrefsFragment.onCreatePreferences()方法。如果你看那裏,它看起來像 -

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

,它應該是以下允許savedInstanceStatenull -

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

+0

好的,所以,除了我完全錯過了它之外,我通常會如何分辨?堆棧跟蹤鏈接到第34行,它近似於'onCreatePreferences'上的一百行...... – Morpheu5

+0

另外,我做了這個改變,並得到完全相同的堆棧跟蹤。 – Morpheu5

+0

哈,我的壞,這次它是'rootKey'(這本身很奇怪,但我想確定,我修正了它)。 – Morpheu5