0

I am using kotlin in my android app and got this crash report:

android.support.v4.app.Fragment$InstantiationException: Unable to instantiate fragment DetailsFragment: make sure class name exists, is public, and has an empty constructor that is public.

Here is my DetailsFragment class.

class DetailsFragment() : Fragment() {

private var workflowId: String? = null
private var workflowData: WorkflowData? = null
private var releaseAtStr: String? = null

public fun init(workflowId: String,
                workflowData: WorkflowData,
                releaseAtStr: String? = null) {
    this.workflowId = workflowId
    this.workflowData = workflowData
    this.releaseAtStr = releaseAtStr
}

private var context: BaseActivity? = null

override fun onAttach(context: Context?) {
    super.onAttach(context)
    if (context is BaseActivity?) {
        this.context = context
    }
}

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

override fun onActivityCreated(savedInstanceState: Bundle?) {
    super.onActivityCreated(savedInstanceState)
    loadingOverlay?.show()
    populateWorkflowHistory()
}

}

Any idea how to fix it or at least reproduce?

Thanks.

M.P.
  • 85
  • 1
  • 11

1 Answers1

0

Try not to call constructor in your fragment:

class DetailsFragment : Fragment() {

private var workflowId: String? = null
private var workflowData: WorkflowData? = null
private var releaseAtStr: String? = null

public fun init(workflowId: String,
                workflowData: WorkflowData,
                releaseAtStr: String? = null) {
    this.workflowId = workflowId
    this.workflowData = workflowData
    this.releaseAtStr = releaseAtStr
}
Levi Moreira
  • 11,917
  • 4
  • 32
  • 46
  • How do mean this? I am quite new. Can you please explain more? – M.P. Apr 13 '18 at 12:58
  • I just removed the ( ) from after DetailsFragment :) – Levi Moreira Apr 13 '18 at 13:05
  • That's the constructor – Levi Moreira Apr 13 '18 at 13:05
  • That means that the constructor from Fragment will be called, am I right? But if I left (), the functionality is the same, just the DetailsFragment constructor call Fragment constructor... – M.P. Apr 16 '18 at 07:03
  • There's an explanation here to why https://stackoverflow.com/questions/10450348/do-fragments-really-need-an-empty-constructor/10450535#10450535 you shouldn't override the constructor in fragments. If you don't declare a constructor for you fragment, a default empty one will be created for you satisfying the requirements stated in the exception – Levi Moreira Apr 16 '18 at 10:45
  • Marked as good answer, I will see if it shows up again. Thanks. – M.P. Apr 16 '18 at 12:14