本文介绍了类扩展片段 - 错误说确保类名称存在,是公开的,有一个空的构造是公开的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样一个类:

class TopicFragment extends Fragment{

public TopicFragment(VO vO) {
    // some code
    }

}

的用户报告说,应用程序崩溃和日志这样说:

Users are reporting that the app is crashing and the log says this:

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

我已经看过这个链接,但我不能够解决这个;

I have looked into this links but i am not able to solve this;

Fragment - InstantiationException:没有空的构造函数 - >谷歌地图V2?

请帮我这个。

推荐答案

只需在不带任何参数是公共的添加一个空的构造。如果您在片段中的XML设置没有它不能创建一个空的构造。

Just simply add an empty constructor with no parameters which is public. if you have the fragment set in XML without an empty constructor it cannot be created.

public TopicFragment() {
}

我平时总是刚刚空构造,并有这样的方法与参数实例

I also usually always have just the empty constructor and have a method like this to instantiate with arguments

public static TopicFragment newInstance(VO vo) {
    TopicFragment fragment = new TopicFragment();
    fragment.setVo(vo);
    return fragment;
}

修改:和球员在评论中说,让你的类:

EDIT: and as the guys said in the comments make your class:

public class TopicFragment {
}

这篇关于类扩展片段 - 错误说确保类名称存在,是公开的,有一个空的构造是公开的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-18 13:30