i have fragment constructor multiple arguments, worked fine during testphase after 300 users downloaded app, have 1 occurence of exception:
android.support.v4.app.fragment$instantiationexception: unable instantiate fragment make sure class name exists, public, , has empty constructor public i mean provide different constructor doesn't make sense since have call method set fragment.
i'm curious why happening sporadically , not , maybe im using fragmented viewpager wrong, because instantiate fragments myself , save them list inside activity. don't use fragmentmanager transaction stuff, since example fragmented viewpager not clear , in end worked fine.
yes do.
you shouldn't overriding constructor anyway. should have newinstance() static method defined , pass parameters via arguments (bundle)
for example:
public static final alertfragment newinstance(int title, string message) { alertfragment f = new alertfragment(); bundle bdl = new bundle(2); bdl.putint(extra_title, title); bdl.putstring(extra_message, message); f.setarguments(bdl); return f; } and of course grabbing args way:
@override public void oncreate(bundle savedinstancestate) { title = getarguments().getint(extra_title); message = getarguments().getstring(extra_message); //... //etc //... } then instantiate fragment manager so:
public oncreate(bundle savedinstancestate) { if(savedinstancestate == null){ getsupportfragmentmanager() .begintransaction() .replace(r.id.content,alertfragment.newinstance( r.string.alert_title, "oh noes error occured!") ) .commit(); } } this way if detached , re-attached object state can stored through arguments. bundles attached intents.
reason - reading
i thought explain why people wondering why.
you see instantiate(..) method in fragment class calls newinstance method.
in case of broken link, can see here: 
http://docs.oracle.com/javase/6/docs/api/java/lang/class.html#newinstance() explains why, upon instantiation checks accessor public , that class loader allows access it.
it's pretty nasty method in all, allows fragmentmanger kill , recreate fragments states. (the android subsystem similar things activities).
example class
i asked alot calling newinstance,(do not confuse class method. whole class example should show usage.
/** * created chris on 21/11/2013 */ public class stationinfoaccessibilityfragment extends basefragment implements journeyproviderlistener { public static final stationinfoaccessibilityfragment newinstance(string crscode) { stationinfoaccessibilityfragment fragment = new stationinfoaccessibilityfragment(); final bundle args = new bundle(1); args.putstring(extra_crs_code, crscode); fragment.setarguments(args); return fragment; } // views linearlayout mlinearlayout; /** * layout inflater */ private layoutinflater minflater; /** * station crs code */ private string mcrscode; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); mcrscode = getarguments().getstring(extra_crs_code); } @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { minflater = inflater; return inflater.inflate(r.layout.fragment_station_accessibility, container, false); } @override public void onviewcreated(view view, bundle savedinstancestate) { super.onviewcreated(view, savedinstancestate); mlinearlayout = (linearlayout)view.findviewby(r.id.station_info_accessibility_linear); //do stuff } @override public void onresume() { super.onresume(); getactivity().getsupportactionbar().settitle(r.string.station_info_access_mobility_title); } // other methods etc... }
Comments
Post a Comment