不断向BackStack添加片段会引发错误

时间:2018-10-16 08:26:47

标签: android android-fragments

我基本上有3个片段。活动加载时显示第一个。有一个活动和三个片段。单击第一个片段中的按钮时,将显示第二个片段。单击第二个片段中的按钮时,将加载第三个片段。 当我将第一个片段添加到后堆栈时,没有问题/错误。但是,当我调用第三个片段并将第二个片段添加到backstack时,它将引发以下错误:

E/AndroidRuntime: FATAL EXCEPTION: main
              Process: com.example.admin.unitconverter, PID: 9384
              java.lang.IllegalArgumentException: Parameter specified as non-null is null: method kotlin.jvm.internal.Intrinsics.checkParameterIsNotNull, parameter view
                  at com.example.admin.unitconverter.SecondFragment$viewSolution$1.onItemSelected(SecondFragment.kt)
                  at android.widget.AdapterView.fireOnSelected(AdapterView.java:931)
                  at android.widget.AdapterView.dispatchOnItemSelected(AdapterView.java:920)
                  at android.widget.AdapterView.selectionChanged(AdapterView.java:914)
                  at android.widget.AdapterView.checkSelectionChanged(AdapterView.java:1092)
                  at android.widget.AdapterView.handleDataChanged(AdapterView.java:1068)
                  at android.widget.AbsSpinner.onMeasure(AbsSpinner.java:181)
                  at android.widget.Spinner.onMeasure(Spinner.java:592)
                  at android.support.v7.widget.AppCompatSpinner.onMeasure(AppCompatSpinner.java:426)
                  at android.view.View.measure(View.java:19857)
                  at android.support.constraint.ConstraintLayout.internalMeasureChildren(ConstraintLayout.java:1227)
                  at android.support.constraint.ConstraintLayout.onMeasure(ConstraintLayout.java:1572)
                  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.support.constraint.ConstraintLayout.internalMeasureChildren(ConstraintLayout.java:1227)
                  at android.support.constraint.ConstraintLayout.onMeasure(ConstraintLayout.java:1572)
                  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:714)
                  at android.support.design.widget.HeaderScrollingViewBehavior.onMeasureChild(HeaderScrollingViewBehavior.java:91)
                  at android.support.design.widget.AppBarLayout$ScrollingViewBehavior.onMeasureChild(AppBarLayout.java:1361)
                  at android.support.design.widget.CoordinatorLayout.onMeasure(CoordinatorLayout.java:784)
                  at android.view.View.measure(View.java:19857)
                  at android.support.v4.widget.DrawerLayout.onMeasure(DrawerLayout.java:1060)
                  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)
                  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)

但是,如果我不将第二个片段添加到Backstack中,则不会引发任何错误,如果我随后运行该应用程序并单击片段3中的“后退”按钮,则该应用程序将省略片段2并跳转到片段1。

这是我的MainActivity

 //val xv = "baseFragment"
    when(fragment){
            "secondfragment" -> {
            toggle.isDrawerIndicatorEnabled = false
            count = count + 1
            //toggle.isDrawerIndicatorEnabled = false
            //frameLayout.removeAllViews()
           // fragmentManager.popBackStack("xv", FragmentManager.POP_BACK_STACK_INCLUSIVE);
            val bundle = Bundle()
            val fragment2 = SecondFragment()
            bundle.putString("buttonClicked","Length")
            fragment2.setArguments(bundle)
            val fragmentManager = getFragmentManager()
            // val ft = fragmentManager.beginTransaction()
            val ft = supportFragmentManager.beginTransaction()
            // Replace the contents of the container with the new fragment
            //ft.remove(null)
            ft.replace(R.id.fragment_container, fragment2)
            ft.addToBackStack("xv")
            ft.commit()

        }
        "searchfragment" -> {
            toggle.isDrawerIndicatorEnabled = false
            count = count + 1
            //toggle.isDrawerIndicatorEnabled = false
           // val bundle = Bundle()
            //frameLayout.removeAllViews()
            val searchFragment = SearchFragment()
           // bundle.putString("buttonClicked","Length")
          // searchFragment.setArguments(bundle);

            val fragmentManager = getFragmentManager()
            // val ft = fragmentManager.beginTransaction()
            val ft = supportFragmentManager.beginTransaction()
            //ft.remove(null)
            // Replace the contents of the container with the new fragment
            ft.replace(R.id.fragment_container, searchFragment)
            ft.addToBackStack(null)
            ft.commit()

        }
    }
if (toolbar != null) {
       /* toggle = ActionBarDrawerToggle(
                this, drawer_layout, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close)
        toggle.syncState()
        drawer_layout.setDrawerListener(toggle)*/
        supportFragmentManager.addOnBackStackChangedListener(object : FragmentManager.OnBackStackChangedListener {
            override fun onBackStackChanged() {
                if (supportFragmentManager.backStackEntryCount > 0) {
                    supportActionBar?.setDisplayHomeAsUpEnabled(true) // show back button
                    toolbar.setNavigationOnClickListener(object : View.OnClickListener {
                        override fun onClick(v: View) {
                            Toast.makeText(getApplicationContext(),"Count Rock"+ count ,Toast.LENGTH_LONG).show()
                           onBackPressed()
                        }
                    })
                } else {
                    //show hamburger
                    supportActionBar?.setDisplayHomeAsUpEnabled(false)
                    toggle.syncState()
                    toolbar.setNavigationOnClickListener(object : View.OnClickListener {
                        override fun onClick(v: View) {
                            drawer_layout.openDrawer(GravityCompat.START)
                        }
                    })
                }
            }
        })

    }

请问我到底在做错什么。任何帮助将不胜感激。谢谢

编辑:以下是@thehrlein要求的“ SecondFragment onItemSelected”

spinner1.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
        override fun onItemSelected(adapterView: AdapterView<*>, view: View, i: Int, l: Long) {
            //Toast.makeText(applicationContext, "This is " + adapterView.getItemAtPosition(i).toString(), Toast.LENGTH_LONG).show()
            // Toast.makeText(applicationContext, "This is " + i, Toast.LENGTH_LONG).show()
            var myAnswer = 0.0
            try {
                //Your task here
                textView1.setText(spinner1.adapter.getItem(i).toString())
                myAnswer = MathParser.eval(replaceOperators(myInput.text.toString()))
                solution(myAnswer.toString())
                spinnerIndex1 = spinner1.selectedItemPosition
                spinnerIndex2 = spinner2.selectedItemPosition
                val editor = sharedPreferences.edit()
                editor.putInt("position1key", spinnerIndex1)
                editor.putInt("position2key", spinnerIndex2)

                //editor.putString("myInput", myInput.text.toString())
                //editor.putString("myResult", result.text.toString())
                editor.apply()
            } catch (e: Exception) {
                e.printStackTrace()
            }
        }

        override fun onNothingSelected(adapterView: AdapterView<*>) {

        }
    }
    spinner2.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
        override fun onItemSelected(adapterView: AdapterView<*>, view: View, i: Int, l: Long) {
            //Toast.makeText(applicationContext, "This is " + adapterView.getItemAtPosition(i).toString(), Toast.LENGTH_LONG).show()
            // Toast.makeText(applicationContext, "This is " + i, Toast.LENGTH_LONG).show()
            var myAnswer = 0.0
            try {
                //Your task here
                textView2.setText(spinner2.adapter.getItem(i).toString())
                myAnswer = MathParser.eval(replaceOperators(myInput.text.toString()))
                solution(myAnswer.toString())
                spinnerIndex1 = spinner1.selectedItemPosition
                spinnerIndex2 = spinner2.selectedItemPosition
                val editor = sharedPreferences.edit()
                editor.putInt("position1key", spinnerIndex1)
                editor.putInt("position2key", spinnerIndex2)
                //editor.putString("myInput", myInput.text.toString())
                //editor.putString("myResult", result.text.toString())
                editor.apply()
            } catch (e: Exception) {
                e.printStackTrace()
            }

        }

        override fun onNothingSelected(adapterView: AdapterView<*>) {

        }
    }

1 个答案:

答案 0 :(得分:0)

首先,我建议您采用其他方法编写一些代码,以免重复做交易事务。

我不明白为什么“ firstfragment”代码为空,而在“ secondfragment”中,您首先从堆栈中弹出,然后替换某些内容?也许

activity?.onBackPressed()

您要搜索的是什么?

在您的日志中,您会收到有关SecondFragment onItemSelected的消息,从而导致问题。如果您还可以发布它的代码,将很有帮助;-)