Class extending Fragment - error says make sure cl

2019-07-28 23:02发布

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.

1条回答
祖国的老花朵
2楼-- · 2019-07-28 23:30

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 {
}
查看更多
登录 后发表回答