fragment使用

最近项目需要新手指导功能,为了图省事Viewpager使用了fragment。因为展示的UI大同小异,所以重载了Fragment的构造方法,这就出事了。

java.lang.RuntimeException: 
Unable to start activity ComponentInfo{ctrip.pad.view/ctrip.pad.splash.NewFunctionIntroduceActivity}: android.support.v4.app.Fragment$InstantiationException: Unable to instantiate fragment ctrip.pad.splash.b: make sure class name exists, is public, and has an empty constructor that is public

看一下官方文档的描述:

Default constructor. Every fragment must have an empty constructor, so it can be instantiated when restoring its activity's state. It is strongly recommended that subclasses do not have other constructors with parameters, since these constructors will not be called when the fragment is re-instantiated; instead, arguments can be supplied by the caller with setArguments(Bundle) and later retrieved by the Fragment with getArguments().

意思就是:极力不建议重写fragment有参数的构造方法。因为fragment被重新加载的时候会调用默认构造方法。就是无参数的构造方法。

提倡这么用fragment:
1
2
3
public static MyFragment getInstance() {
        return new MyFragment();
    }
 
 

 

 





posted @ 2013-10-09 09:58  Cyning  阅读(704)  评论(0)    收藏  举报