android - Changing fragment from Navigation drawer -


i having exact same problem op here: how change fragments using android navigation drawer

also using template navigation drawer activity in android studio.

i tried dreagan's answer, stuck cycle of errors.

here code

import android.app.activity; import android.app.actionbar; import android.app.fragment; import android.app.fragmentmanager; import android.app.fragmenttransaction; import android.content.context; import android.os.build; import android.os.bundle; import android.view.gravity; import android.view.layoutinflater; import android.view.menu; import android.view.menuitem; import android.view.view; import android.view.viewgroup; import android.support.v4.widget.drawerlayout; import android.widget.arrayadapter; import android.widget.textview;   public   class mainactivity extends activity         implements navigationdrawerfragment.navigationdrawercallbacks {      /**      * fragment managing behaviors, interactions , presentation of navigation drawer.      */     private navigationdrawerfragment mnavigationdrawerfragment;      /**      * used store last screen title. use in {@link #restoreactionbar()}.      */     private charsequence mtitle;      @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.activity_main);          mnavigationdrawerfragment = (navigationdrawerfragment)                 getfragmentmanager().findfragmentbyid(r.id.navigation_drawer);         mtitle = gettitle();          // set drawer.         mnavigationdrawerfragment.setup(                 r.id.navigation_drawer,                 (drawerlayout) findviewbyid(r.id.drawer_layout));     }      @override     public void onnavigationdraweritemselected(int position) {         // update main content replacing fragments          fragment fragment = new newsfeedfragment();         fragmentmanager fragmentmanager = getfragmentmanager();          bundle args = new bundle();         args.putint(newsfeedfragment.arg_planet_number, position);         fragment.setarguments(args);          switch(position){             case 0:                 fragment = new newsfeedfragment();                  break;              case 1:                 fragment = new newsfeedfragment();                 break;         }         fragmentmanager.begintransaction()                 .replace(r.id.container, fragment)                 .commit();     }      public void onsectionattached(int number) {          switch (number) {             case 1:                 mtitle = getstring(r.string.title_section1);                 break;              case 2:                 mtitle = getstring(r.string.title_section2);                 break;              case 3:                 mtitle = getstring(r.string.title_section3);                 break;         }     }      public void restoreactionbar() {         actionbar actionbar = getactionbar();         actionbar.setnavigationmode(actionbar.navigation_mode_standard);         actionbar.setdisplayshowtitleenabled(true);         actionbar.settitle(mtitle);     }       @override     public boolean oncreateoptionsmenu(menu menu) {         if (!mnavigationdrawerfragment.isdraweropen()) {             // show items in action bar relevant screen             // if drawer not showing. otherwise, let drawer             // decide show in action bar.             getmenuinflater().inflate(r.menu.main, menu);             restoreactionbar();             return true;         }         return super.oncreateoptionsmenu(menu);     }      @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);     }      /**      * placeholder fragment containing simple view.      */     public static class placeholderfragment extends fragment {         /**          * fragment argument representing section number          * fragment.          */         private static final string arg_section_number = "section_number";          /**          * returns new instance of fragment given section          * number.          */         public static placeholderfragment newinstance(int sectionnumber) {             placeholderfragment fragment = new placeholderfragment();             bundle args = new bundle();             args.putint(arg_section_number, sectionnumber);             fragment.setarguments(args);             return fragment;         }          public placeholderfragment() {         }          @override         public view oncreateview(layoutinflater inflater, viewgroup container,                                  bundle savedinstancestate) {             view rootview = inflater.inflate(r.layout.fragment_main, container, false);             return rootview;         }          @override         public void onattach(activity activity) {             super.onattach(activity);             ((mainactivity) activity).onsectionattached(                     getarguments().getint(arg_section_number));         }     }  } 

if run code here following error:

java.lang.runtimeexception: unable start activity componentinfo{dk.gruppe2.jammerbugtevent/dk.gruppe2.jammerbugtevent.mainactivity}: java.lang.classcastexception: dk.gruppe2.jammerbugtevent.mainactivity@429e4a70 must implement onfragmentinteractionlistener 

when implement onfragmentinteractionlistener, following error:

error:(22, 9) error: mainactivity not abstract , not override abstract method onfragmentinteraction(uri) in onfragmentinteractionlistener 

when declare mainactivity abstract, error:

    java.lang.runtimeexception: unable instantiate activity componentinfo{dk.gruppe2.jammerbugtevent/dk.gruppe2.jammerbugtevent.mainactivity}: java.lang.instantiationexception: can't instantiate class dk.gruppe2.jammerbugtevent.mainactivity 

i new android, , not expert regarding programming begin with. appreciated. in advance.

here explanation of things!

reading code did not see class cast exception... casting activity interface onfragmentinteractionlistener. maybe in 1 of fragments.

also:

when implement onfragmentinteractionlistener, following error:

error:(22, 9) error: mainactivity not abstract , not override abstract method onfragmentinteraction(uri) in onfragmentinteractionlistener

when implement onfragmentinteractionlistener must override method: onfragmentinteraction(uri).


Comments

Popular posts from this blog

matlab - "Contour not rendered for non-finite ZData" -

delphi - Indy UDP Read Contents of Adata -

javascript - Any ideas when Firefox is likely to implement lengthAdjust and textLength? -