android - How to call a fragment from a fragment -
i making application. in application, have fragment contains listview. when click on item should start new fragment displays item website. listview fragment code :
public class myfragment extends fragment { private listview mlistviewassociation; private listviewassociationadapter mlistviewassociationadapter; private arraylist<listviewassociations> mlistviewlist; private progressdialog progressdialog; public myfragment () { // required empty public constructor } @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { // inflate layout fragment view rootview = inflater.inflate(r.layout.fragment_mwl_associations, container, false); mlistviewassociation = (listview) rootview.findviewbyid(r.id.association_list); mlistviewlist = new arraylist<listviewassociations>(); string[] massciation_name ; final string webs[]; typedarray icons ; massciation_name = getresources().getstringarray(r.array.association_names); webs = getresources().getstringarray(r.array.association_websites) ; icons = getresources().obtaintypedarray(r.array.association_icons); listviewassociations mytitle ; for(int = 0 ; i<massciation_name.length ; i++) { mytitle = new listviewassociations(massciation_name[i],icons.getresourceid(i,r.drawable.ic_launcher)); mlistviewlist.add(mytitle); } mlistviewassociationadapter = new listviewassociationadapter(getactivity(),mlistviewlist); mlistviewassociation.setadapter(mlistviewassociationadapter);
here handling on clicking on item
mlistviewassociation.setonitemclicklistener(new adapterview.onitemclicklistener() { @override public void onitemclick(adapterview<?> parent, view view, int position, long id) { blankfragment myfragment = new blankfragment(); fragmentmanager fragmentmanager = getfragmentmanager(); fragmenttransaction fragmenttransaction = fragmentmanager.begintransaction(); fragmenttransaction.replace(r.layout.fragment_mwl_associations, myfragment); fragmenttransaction.addtobackstack(null); fragmenttransaction.commit(); } }); return rootview ; }
my blankfragment called :
public class blankfragment extends fragment { private progressdialog progressdialog; webview mywebview; private string url ; public blankfragment() { // required empty public constructor } public void seturl(string url) { this.url = url; } public string geturl() { return url; } @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { // inflate layout fragment view rootview = inflater.inflate(r.layout.fragment_blank, container, false); webview mywebview = (webview) rootview.findviewbyid(r.id.frag_blank); mywebview.setwebviewclient(new webviewclient()); mywebview.getsettings().setbuiltinzoomcontrols(true); mywebview.requestfocusfromtouch(); mywebview.setverticalscrollbarenabled(true); mywebview.sethorizontalscrollbarenabled(true); mywebview.setverticalscrollbarenabled(true); mywebview.sethorizontalscrollbarenabled(true); mywebview.requestfocusfromtouch(); mywebview.getsettings().setjavascriptenabled(true); mywebview.getsettings().setusewideviewport(true); mywebview.getsettings().setloadwithoverviewmode(true); mywebview.addjavascriptinterface(new webappinterface(getactivity()), "android"); mywebview.getsettings().setjavascriptenabled(true); mywebview.loadurl("www.google.com"); new loadviewtask().execute(); return rootview ; }
when click on item application crashes. mistake ??
the mistake have done in onitemclick()
in line : change
fragmenttransaction.replace(r.layout.fragment_mwl_associations, myfragment);
to
fragmenttransaction.replace(r.id.container, myfragment);
here, entered wrong layout id replace, instead add framelayout
container id r.id.container
have used add fragment
in mainactivity.java
.
reference links more helpful: fragments basics
Comments
Post a Comment