KOTLIN - > InstantiationException:无法实例化片段确保类名存在,是公共的,并且具有公共的

时间:2018-04-13 09:51:35

标签: android android-fragments kotlin kotlin-android-extensions

我在我的Android应用程序中使用kotlin并获得此崩溃报告:

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.

这是我的DetailsFragment类。

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()
}

}

知道如何修复或至少重现吗?

感谢。

1 个答案:

答案 0 :(得分:0)

尽量不要在片段中调用构造函数:

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
}