java - Unable To Create CardView Fragment -
i have no idea on wrong code.
my application using navigation drawer. there 4 different fragments , 1 of cardview fragment have converted earlier activity fragment.
the cardview fragment being underlined red shows error not able compiled. kindly refer attached pictures.
if accept proposed solutions android studio,it solve cardviewfragment , new problem occur @ other 3 fragments.
and both main activity , cardview fragment.
mainactivity.java
package info.androidhive.materialdesign.activity; import android.os.bundle; import android.support.v4.app.fragment; import android.support.v4.app.fragmentmanager; import android.support.v4.app.fragmenttransaction; import android.support.v4.widget.drawerlayout; import android.support.v7.app.appcompatactivity; import android.support.v7.widget.toolbar; import android.view.menu; import android.view.menuitem; import android.view.view; import android.widget.toast; import info.androidhive.materialdesign.r; public class mainactivity extends appcompatactivity implements fragmentdrawer.fragmentdrawerlistener { private static string tag = mainactivity.class.getsimplename(); private toolbar mtoolbar; private fragmentdrawer drawerfragment; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); mtoolbar = (toolbar) findviewbyid(r.id.toolbar); setsupportactionbar(mtoolbar); getsupportactionbar().setdisplayshowhomeenabled(true); drawerfragment = (fragmentdrawer) getsupportfragmentmanager().findfragmentbyid(r.id.fragment_navigation_drawer); drawerfragment.setup(r.id.fragment_navigation_drawer, (drawerlayout) findviewbyid(r.id.drawer_layout), mtoolbar); drawerfragment.setdrawerlistener(this); // display first navigation drawer view on app launch displayview(0); } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.menu_main, menu); return true; } @override public boolean onoptionsitemselected(menuitem item) { // handle action bar item clicks here. action bar // automatically handle clicks on home/up button, long // specify parent activity in androidmanifest.xml. int id = item.getitemid(); //noinspection simplifiableifstatement if (id == r.id.action_settings) { return true; } if(id == r.id.action_search){ toast.maketext(getapplicationcontext(), "search action selected!", toast.length_short).show(); return true; } return super.onoptionsitemselected(item); } @override public void ondraweritemselected(view view, int position) { displayview(position); } private void displayview(int position) { fragment fragment = null; string title = getstring(r.string.app_name); switch (position) { case 0: fragment = new homefragment(); title = getstring(r.string.title_home); break; case 1: fragment = new friendsfragment(); title = getstring(r.string.title_friends); break; case 2: fragment = new messagesfragment(); title = getstring(r.string.title_messages); break; case 3: fragment = new cardviewfragment(); title = getstring(r.string.title_messages); break; default: break; } if (fragment != null) { fragmentmanager fragmentmanager = getsupportfragmentmanager(); fragmenttransaction fragmenttransaction = fragmentmanager.begintransaction(); fragmenttransaction.replace(r.id.container_body, fragment); fragmenttransaction.commit(); // set toolbar title getsupportactionbar().settitle(title); } } }
cardviewfragment.java
package info.androidhive.materialdesign.activity; import android.app.activity; import android.app.fragment; import android.os.bundle; import android.support.v4.app.fragmentactivity; import android.support.v7.app.actionbaractivity; import android.support.v7.widget.cardview; import android.support.v7.widget.gridlayoutmanager; import android.support.v7.widget.linearlayoutmanager; import android.support.v7.widget.recyclerview; import android.support.v7.widget.staggeredgridlayoutmanager; import android.support.v7.widget.toolbar; import android.view.layoutinflater; import android.view.menu; import android.view.menuitem; import android.view.view; import android.view.viewgroup; import android.widget.linearlayout; import java.util.arraylist; import info.androidhive.materialdesign.r; public class cardviewfragment extends fragment { private toolbar toolbar; private recyclerview recyclerview; private cardview cardview; private arraylist<feddproperties> os_versions; private recyclerview.adapter madapter; // private recyclerview.layoutmanager mlayoutmanager; private linearlayout lllayout; private fragmentactivity faactivity; @override /*protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); initcontrls(); }*/ public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate){ faactivity=(fragmentactivity) super.getactivity(); lllayout=(linearlayout) inflater.inflate(r.layout.activity_main, container, false); initcontrls(); return lllayout; } private void initcontrls() { //toolbar = (toolbar) findviewbyid(r.id.toolbar); // cardview = (cardview) findviewbyid(r.id.cardlist); recyclerview = (recyclerview) lllayout.findviewbyid(r.id.my_recycler_view); /*if (toolbar != null) { setsupportactionbar(toolbar); getsupportactionbar().settitle("android versions"); }*/ final string[] versions = {"alpha", "beta", "cupcake", "donut", "eclair", "froyo", "gingerbread", "honeycomb", "ice cream sandwitch", "jellybean", "kitkat", "lollypop"}; final int[] icons = {r.drawable.ic_launcher, r.drawable.ic_launcher, r.drawable.ic_launcher, r.drawable.donut, r.drawable.eclair, r.drawable.froyo, r.drawable.gingerbread, r.drawable.honeycomb, r.drawable.icecream_sandwhich, r.drawable.jellybean, r.drawable.kitkat, r.drawable.lollipop}; os_versions = new arraylist<feddproperties>(); (int = 0; < versions.length; i++) { feddproperties feed = new feddproperties(); feed.settitle(versions[i]); feed.setthumbnail(icons[i]); os_versions.add(feed); } recyclerview.sethasfixedsize(true); // listview recyclerview.setlayoutmanager(new linearlayoutmanager(getactivity())); //grid view // recyclerview.setlayoutmanager(new gridlayoutmanager(this,2,1,false)); //staggeredgridview // recyclerview.setlayoutmanager(new staggeredgridlayoutmanager(2,1)); // create object adapter madapter = new cardviewdataadapter(os_versions); // set adapter object recyclerview recyclerview.setadapter(madapter); } /* @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.menu_main, menu); return true; } @override public boolean onoptionsitemselected(menuitem item) { // handle action bar item clicks here. action bar // automatically handle clicks on home/up button, long // specify parent activity in androidmanifest.xml. int id = item.getitemid(); //noinspection simplifiableifstatement if (id == r.id.action_settings) { return true; } return super.onoptionsitemselected(item); }*/ }
in mainactivity.java, declared
import android.support.v4.app.fragment
import same type of fragment in cardviewfragment.java instead of
import android.app.fragment;
so it's clear same type of fragment being used in both pieces of code.
Comments
Post a Comment