简体   繁体   中英

android - How to set a Fragment on Activity OnCreate? (Kotlin)

Im currently working on an app which has a Settings Activity. In this activity, there is a FrameLayout, that loads the Settings Main fragment.

When i try to load the fragment in onCreate of the activity using a function that contains the supportFragmentManager i get the error Unable to start activity: java.lang.IllegalStateException: Fragment not attached to a context . The weird thing is, Android Studio, points at val frgSettingsMain = FragmentSettingsMain() and DataItemsSettings .

What am i doing wrong? Are there any other cleaner ways to initialize a Fragment in onCreate?

Code:

ActivitySettings.kt

class ActivitySettings : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_settings)

        topToolbarBack.setNavigationOnClickListener {
            finish()
        }

        val frgSettingsMain = FragmentSettingsMain()

        setCurrentFragment(frgSettingsMain)

    }

    private fun setCurrentFragment(fragment: Fragment) =
        supportFragmentManager.beginTransaction().apply {
            replace(R.id.framelayoutSettings, fragment)
            commit()
        }
}

FragmentSettingsMain.kt

class FragmentSettingsMain : Fragment(), AdapterSettings.OnItemClickListener {
    val settingsList = listOf(
        DataItemsSettings(getString(R.string.look), getString(R.string.lookdescription), R.drawable.ic_colored_color_lens),
        DataItemsSettings(getString(R.string.playing), getString(R.string.playingdescription), R.drawable.ic_colored_view_carousel),
        DataItemsSettings(getString(R.string.images), getString(R.string.imagesdscription), R.drawable.ic_colored_image),
        DataItemsSettings(getString(R.string.audio), getString(R.string.audiodescription), R.drawable.ic_colored_volume_up),
        DataItemsSettings(getString(R.string.other), getString(R.string.otherdescription), R.drawable.ic_colored_shape),
        DataItemsSettings(getString(R.string.about), getString(R.string.aboutdescription), R.drawable.ic_colored_info)
    )

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        retainInstance = true
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? = inflater.inflate(R.layout.fragment_settings_main, container, false)

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

        rvSettings.apply {
            layoutManager = LinearLayoutManager(activity)
            adapter = AdapterSettings(settingsList, this@FragmentSettingsMain)
        }
    }

    override fun OnItemClick(position: Int) {
        when(position) {
            0 -> Toast.makeText(context, "Pressed Option 0", Toast.LENGTH_LONG).show()
            1 -> Toast.makeText(context, "Pressed Option 1", Toast.LENGTH_LONG).show()
            2 -> Toast.makeText(context, "Pressed Option 2", Toast.LENGTH_LONG).show()
            3 -> Toast.makeText(context, "Pressed Option 3", Toast.LENGTH_LONG).show()
            4 -> Toast.makeText(context, "Pressed Option 4", Toast.LENGTH_LONG).show()
            5 -> Toast.makeText(context, "Pressed Option 5", Toast.LENGTH_LONG).show()
        }
    }
}

The problem is that method getstring() needs context. The context is not attached to the Fragment when you call FragmentSettingsMain(). Try to initialize variable settingsList after the Fragment is added to the container. For example:

lateinit var settingsList : List<DataItemSettings>
override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        retainInstance = true
        settingsList = listOf(
        DataItemsSettings(getString(R.string.look), getString(R.string.lookdescription), R.drawable.ic_colored_color_lens),
        DataItemsSettings(getString(R.string.playing), getString(R.string.playingdescription), R.drawable.ic_colored_view_carousel),
        DataItemsSettings(getString(R.string.images), getString(R.string.imagesdscription), R.drawable.ic_colored_image),
        DataItemsSettings(getString(R.string.audio), getString(R.string.audiodescription), R.drawable.ic_colored_volume_up),
        DataItemsSettings(getString(R.string.other), getString(R.string.otherdescription), R.drawable.ic_colored_shape),
        DataItemsSettings(getString(R.string.about), getString(R.string.aboutdescription), R.drawable.ic_colored_info)
    )

    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM