Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to fix not attached to an activity in Android

In my application I want use this library for ImagePicker : https://github.com/SimformSolutionsPvtLtd/SSImagePicker

But after add this line private val imagePicker: ImagePicker = registerImagePicker(this) show me Force close.

I want use this into Fragment and this fragment info ViewPager2 .
FragmentAdapter code :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

class FragmentsAdapter (fragmentManager: FragmentManager, lifecycle: Lifecycle) :
    FragmentStateAdapter(fragmentManager, lifecycle) {

    override fun getItemCount(): Int {
        return 2
    }

    override fun createFragment(position: Int): Fragment {
        when (position) {
            0 -> return FragmentA()
        }
        return FragmentB()
    }
}

FragmentB codes :

class FragmentB : Fragment(), ImagePickerResultListener {

private val imagePicker: ImagePicker = registerImagePicker(this)

...

    private fun openImagePicker() {
        imagePicker
            .title("Images")
            .multipleSelection(false)
            .showCountInToolBar(true)
            .showFolder(true)
            .cameraIcon(true)
            .doneIcon(true)
            .allowCropping(true)
            .compressImage(true)
            .maxImageSize(2.5f)
            .extension(PickExtension.ALL)
        imagePicker.open(PickerType.GALLERY)
    }

}

Error message :

java.lang.IllegalStateException: Fragment MandatoryInfoFragment{ccb6b52} (d0165abf-9555-41a8-a139-d84da8774350) not attached to an activity.
    at androidx.fragment.app.Fragment.requireActivity(Fragment.java:995)
    at com.app.imagepickerlibrary.ImagePicker$Companion.registerImagePicker(ImagePicker.kt:232)
    at com.myapp.ui.login.pages.complete.mandatory.MandatoryInfoFragment.<init>(FragmentB.kt:51)
    at com.myapp.ui.login.pages.complete.FragmentsAdapter.createFragment(FragmentsAdapter.kt:22)

How can I fix it?

>Solution :

The reason is registerImagePicker is calling requireActivity() to get the Context . And at the time of initialization requireActivity() will return null because the Fragment is not attached yet to Activity. See the code below.

 fun Fragment.registerImagePicker(callback: ImagePickerResultListener): ImagePicker {
        return ImagePicker(callback, requireActivity())
    }

To fix this you can move the initialization down in lifecycle Tree of Fragment after the fragment is attached for example inside onViewCreated() . Check fragment lifecycle.

class FragmentB : Fragment(), ImagePickerResultListener {

private lateinit var imagePicker: ImagePicker

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)
    imagePicker = registerImagePicker(this)
}
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading