简体   繁体   中英

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 {
}

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