努力将代码从 AppcompatActivity 转换为 Fragment。无法使用 setSupportActionBar(工具栏)

Struggling to transition code from AppcompatActivity to Fragment. Unable to use setSupportActionBar(toolbar)

我正在尝试从使用 AppcomatActivity 过渡到使用 Fragment,因为我正在更新我的应用程序,但 运行 遇到了问题。当我尝试 运行 我的应用程序时,我收到 “未解决的参考:supportFragmentManager”和“未解决的参考:setSupportActionBar”(这就是 Logcat 向我显示的所有内容)。现在我选择改用fragments,因为我也想换appsUI,使用起来比之前的版本快。不管怎样,这是我的代码:

录音机片段

    package com.khumomashapa.notes.fragments

import android.os.Build
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toolbar
import androidx.annotation.RequiresApi
import androidx.fragment.app.FragmentManager
import androidx.fragment.app.FragmentPagerAdapter
import androidx.viewpager.widget.ViewPager
import com.astuetz.PagerSlidingTabStrip
import com.khumomashapa.notes.R
import kotlinx.android.synthetic.main.toolbar3.*


class RecorderFragment : Fragment() {

    private var tabs: PagerSlidingTabStrip? = null
    private var pager: ViewPager? = null

    @RequiresApi(Build.VERSION_CODES.LOLLIPOP)
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        activity?.title = "Recorder";

        pager = pager?.findViewById<View>(R.id.pager) as ViewPager
        pager!!.adapter = MyAdapter(supportFragmentManager)
        tabs = tabs?.findViewById<View>(R.id.tabs) as PagerSlidingTabStrip
        tabs!!.setViewPager(pager)
        val toolbar = toolbar?.findViewById<View>(R.id.toolbar) as Toolbar
        toolbar.popupTheme = R.style.ThemeOverlay_AppCompat_Light
        setSupportActionBar(toolbar)




    }


    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {



        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_recorder, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

    }

    inner class MyAdapter(fm: FragmentManager?) : FragmentPagerAdapter(
        fm!!
    ) {
        private val titles = arrayOf(
            getString(R.string.tab_title_record),
            getString(R.string.tab_title_saved_recordings)
        )

        override fun getItem(position: Int): Fragment {
            when (position) {
                0 -> {
                    return RecordFragment.newInstance(position)
                }
                1 -> {
                    return FileViewerFragment.newInstance(position)
                }
            }
            return null!!
        }

        override fun getCount(): Int {
            return titles.size
        }

        override fun getPageTitle(position: Int): CharSequence? {
            return titles[position]

        }
    }

    companion object {
        private val LOG_TAG = RecorderFragment::class.java.simpleName
    }
}

这个 class 的目的是显示一个视图寻呼机,它可以在我已经创建的其他片段之间切换。我能够修复与此相关的其他错误,例如“未解决的 findViewById 引用”和“MyAdapter class”。

对于一个Fragment,实际上有两个相关的片段管理器。决定使用哪一个取决于您的用例。

Child 片段管理器

一个片段有一个child片段管理器负责管理它的child/nested片段。您可以通过以下方式获得:

Fragment.getChildFragmentManager()

Parent 的片段管理器

片段还持有对其 parent 片段管理器的引用。如果片段是 activity 的直接 child 那么他代表活动片段管理器。否则,如果该片段是另一个片段的 child,则它表示 parent 片段的 child 片段管理器。这可以通过以下方式获得:

Fragment.getParentFragmentManager()

请注意,虽然 Fragment 有方法 Fragment.getFragmentManager(),但已弃用 Fragment.getParentFragmentManager(),因此不应使用它。

您也可以通过 Fragment.getAcitivity() 获取对片段 activity 的引用,然后调用 Activity.getSupportFragmentManager() 从技术上获取活动片段管理器。但通常 parent 片段管理器更有用和清晰。