简体   繁体   中英

KOTLIN -> InstantiationException: Unable to instantiate fragment make sure class name exists, is public, and has an empty constructor that is public

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.

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
}

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