当前位置: 动力学知识库 > 问答 > 编程问答 >

android - Class extending Fragment - error says make sure class name exists, is public, and has an empty constructor that is public

问题描述:

I have a class like this:

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;

Do fragments really need an empty constructor?

Fragment - InstantiationException: no empty Constructor -> Google Maps v2?

Please help me with this.

网友答案:

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 {
}
分享给朋友:
您可能感兴趣的文章:
随机阅读: