Fragment must be a public static class to be properly recreated from instance state.

继承Fragment的类必须为public static class

错误的可能原因:

        1、继承Fragment的类用inner修饰

   inner class MyFragment() : Fragment() {
        override fun onCreateView(
            inflater: LayoutInflater,
            container: ViewGroup?,
            savedInstanceState: Bundle?
        ): View? {
           ...
        }
    }

        2、继承Fragment的类定位为匿名内部类

    fun setFragment(position: Int) {
        val fm: FragmentManager = supportFragmentManager //得到FragmentManager
        val ft: FragmentTransaction = fm.beginTransaction() //开启事务
        ft.replace(R.id.fl_main, object :Fragment(){
            override fun onCreateView(
                inflater: LayoutInflater,
                container: ViewGroup?,
                savedInstanceState: Bundle?
            ): View? {
                ...
            }
        })
        //替换Fragment
        ft.commit() //提交
    }

正确的做法应该是:

    class MyFragment() : Fragment() {
        override fun onCreateView(
            inflater: LayoutInflater,
            container: ViewGroup?,
            savedInstanceState: Bundle?
        ): View? {
            ...
        }
    }


  fun setFragment(position: Int) {
        val fm: FragmentManager = supportFragmentManager //得到FragmentManager
        val ft: FragmentTransaction = fm.beginTransaction() //开启事务
        ft.replace(R.id.fl_main, MyFragment())
        //替换Fragment
        ft.commit() //提交
    }