java - why am i getting a "cannot resolve method" error on getApplication? -
i believe have code in order ive been toiling on error few hours now. im trying setup basic app framework using fragments. can code work if structure project activity based , put string data along block of code below in mainactivity, doesnt me try move them fragments.
in mainactivity (where same block of code works without using fragments) placed in oncreate makes no difference if place there in fragment , read placing in oncreate not proper place.
im new android development, if explain answer code example greatly.
thank taking time look.
my entire fragment below.
public class basicspagefragment extends fragment { recyclerview recyclerview; private recycler_view_adapter adapter; public basicspagefragment() { // required empty public constructor } @override public void oncreate(bundle savedinstancestate) { } @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { // inflate layout view rootview = inflater.inflate(r.layout.recycler_view, container, false); recyclerview rv = (recyclerview) rootview.findviewbyid(r.id.fragment_container); rv.sethasfixedsize(true); linearlayoutmanager llm = new linearlayoutmanager(getactivity()); rv.setlayoutmanager(llm); return rootview; } @override protected void onviewcreated(bundle savedinstancestate) { // below receiving error (cannot resolve method 'getapplication()' // line of code above throwing "getview() in fragment cannot applied" // , savedinstancestate says "onviewcreated (android.view.view, bundle) // in fragment cannot applied (android.os.bundle)" super.onviewcreated(savedinstancestate); list<cardviewdata> data = fill_with_data(); recyclerview = (recyclerview) getview(r.id.fragment_container); adapter = new recycler_view_adapter(data, getapplication()); recyclerview.setadapter(adapter); recyclerview.setlayoutmanager(new linearlayoutmanager(this)); // errors stop here } //create list of cardviewdata objects public list<cardviewdata> fill_with_data() { list<cardviewdata> data = new arraylist<>(); data.add(new cardviewdata("batman vs superman", "following destruction of metropolis, batman embarks on personal vendetta against superman ", r.drawable.ic_action_movie)); data.add(new cardviewdata("x-men: apocalypse", "x-men: apocalypse upcoming american superhero film based on x-men characters appear in marvel comics ", r.drawable.ic_action_movie)); data.add(new cardviewdata("captain america: civil war", "a feud between captain america , iron man leaves avengers in turmoil. ", r.drawable.ic_action_movie)); data.add(new cardviewdata("kung fu panda 3", "after reuniting long-lost father, po must train village of pandas", r.drawable.ic_action_movie)); data.add(new cardviewdata("warcraft", "fleeing dying home colonize another, fearsome orc warriors invade peaceful realm of azeroth. ", r.drawable.ic_action_movie)); data.add(new cardviewdata("alice in wonderland", "alice in wonderland: through looking glass ", r.drawable.ic_action_movie)); return data; } }
in case need adapter...
public class recycler_view_adapter extends recyclerview.adapter<cardviewviewholder> { list<cardviewdata> list = collections.emptylist(); context context; public recycler_view_adapter(list<cardviewdata> list, context context) { this.list = list; this.context = context; } @override public cardviewviewholder oncreateviewholder(viewgroup parent, int viewtype) { view v = layoutinflater.from(parent.getcontext()).inflate(r.layout.cardview_layout, parent, false); cardviewviewholder holder = new cardviewviewholder(v); return holder; } @override public void onbindviewholder(cardviewviewholder holder, int position) { holder.title.settext(list.get(position).title); holder.description.settext(list.get(position).description); holder.imageview.setimageresource(list.get(position).imageid); } @override public int getitemcount() { return list.size(); } @override public void onattachedtorecyclerview(recyclerview recyclerview) { super.onattachedtorecyclerview(recyclerview); } // insert new item recyclerview public void insert(int position, cardviewdata data) { list.add(position, data); notifyiteminserted(position); } // remove recyclerview item containing cardviewdata object public void remove(cardviewdata data) { int position = list.indexof(data); list.remove(position); notifyitemremoved(position); } }
change line
adapter = new recycler_view_adapter(data, getapplication());
to this
adapter = new recycler_view_adapter(data, getactivity());
Comments
Post a Comment