android - InflateException when inflating a fragment for a drawer caused by NullPointerException thrown by setContentView() in MainActivity -


since android studio came out decided import project in eclipse. app crashes in api 16 works fine 21. minimum api 14 , maximum 21, can't try running @ in between because don't have device @ level , emulator won't run it. wasn't using gradle on eclipse in android studio.

i'd love helping hand cause it's bit i'm stuck on problem.

you may find code shitty , redundant, i'm sorry in advance please focus on main problem.

and thank you!

stacktrace

28073-28073/sc.erza.prancer e/androidruntime﹕ fatal exception: main     java.lang.runtimeexception: unable start activity componentinfo{sc.erza.prancer/sc.erza.prancer.mainactivity}: android.view.inflateexception: binary xml file line #33: error inflating class fragment             @ android.app.activitythread.performlaunchactivity(activitythread.java:2085)             @ android.app.activitythread.handlelaunchactivity(activitythread.java:2110)             @ android.app.activitythread.access$600(activitythread.java:138)             @ android.app.activitythread$h.handlemessage(activitythread.java:1205)             @ android.os.handler.dispatchmessage(handler.java:99)             @ android.os.looper.loop(looper.java:137)             @ android.app.activitythread.main(activitythread.java:4940)             @ java.lang.reflect.method.invokenative(native method)             @ java.lang.reflect.method.invoke(method.java:511)             @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:798)             @ com.android.internal.os.zygoteinit.main(zygoteinit.java:565)             @ dalvik.system.nativestart.main(native method)      caused by: android.view.inflateexception: binary xml file line #33: error inflating class fragment             @ android.view.layoutinflater.createviewfromtag(layoutinflater.java:704)             @ android.view.layoutinflater.rinflate(layoutinflater.java:746)             @ android.view.layoutinflater.inflate(layoutinflater.java:489)             @ android.view.layoutinflater.inflate(layoutinflater.java:396)             @ android.view.layoutinflater.inflate(layoutinflater.java:352)             @ com.android.internal.policy.impl.phonewindow.setcontentview(phonewindow.java:425)             @ android.app.activity.setcontentview(activity.java:2122)             @ sc.erza.prancer.mainactivity.oncreate(mainactivity.java:69)             @ android.app.activity.performcreate(activity.java:5275)             @ android.app.instrumentation.callactivityoncreate(instrumentation.java:1082)             @ android.app.activitythread.performlaunchactivity(activitythread.java:2049)             at android.app.activitythread.handlelaunchactivity(activitythread.java:2110)             at android.app.activitythread.access$600(activitythread.java:138)             at android.app.activitythread$h.handlemessage(activitythread.java:1205)             at android.os.handler.dispatchmessage(handler.java:99)             at android.os.looper.loop(looper.java:137)             at android.app.activitythread.main(activitythread.java:4940)             at java.lang.reflect.method.invokenative(native method)             at java.lang.reflect.method.invoke(method.java:511)             at com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:798)             at com.android.internal.os.zygoteinit.main(zygoteinit.java:565)             at dalvik.system.nativestart.main(native method)      caused by: java.lang.nullpointerexception             @ sc.erza.prancer.navigationdrawerfragment.oncreateview(navigationdrawerfragment.java:104)             @ android.app.fragmentmanagerimpl.movetostate(fragmentmanager.java:807)             @ android.app.fragmentmanagerimpl.movetostate(fragmentmanager.java:1013)             @ android.app.fragmentmanagerimpl.addfragment(fragmentmanager.java:1112)             @ android.app.activity.oncreateview(activity.java:4931)             @ android.view.layoutinflater.createviewfromtag(layoutinflater.java:680)             at android.view.layoutinflater.rinflate(layoutinflater.java:746)             at android.view.layoutinflater.inflate(layoutinflater.java:489)             at android.view.layoutinflater.inflate(layoutinflater.java:396)             at android.view.layoutinflater.inflate(layoutinflater.java:352)             at com.android.internal.policy.impl.phonewindow.setcontentview(phonewindow.java:425)             at android.app.activity.setcontentview(activity.java:2122)             at sc.erza.prancer.mainactivity.oncreate(mainactivity.java:69)             at android.app.activity.performcreate(activity.java:5275)             at android.app.instrumentation.callactivityoncreate(instrumentation.java:1082)             at android.app.activitythread.performlaunchactivity(activitythread.java:2049)             at android.app.activitythread.handlelaunchactivity(activitythread.java:2110)             at android.app.activitythread.access$600(activitythread.java:138)             at android.app.activitythread$h.handlemessage(activitythread.java:1205)             at android.os.handler.dispatchmessage(handler.java:99)             at android.os.looper.loop(looper.java:137)             at android.app.activitythread.main(activitythread.java:4940)             at java.lang.reflect.method.invokenative(native method)             at java.lang.reflect.method.invoke(method.java:511)             at com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:798)             at com.android.internal.os.zygoteinit.main(zygoteinit.java:565)             at dalvik.system.nativestart.main(native method) 

mainactivity.java

i cut part of activity away since wasn't important, contained 3 fragments code never reached.

package sc.erza.prancer;  import android.app.actionbar; import android.app.activity; import android.app.fragment; import android.app.fragmentmanager; import android.content.contentvalues; import android.content.intent; import android.database.cursor; import android.database.sqlexception; import android.database.sqlite.sqlitedatabase; import android.database.sqlite.sqliteexception; import android.os.bundle; import android.support.v4.widget.drawerlayout; import android.util.log; import android.view.actionmode; import android.view.layoutinflater; import android.view.menu; import android.view.menuinflater; import android.view.menuitem; import android.view.view; import android.view.viewgroup; import android.widget.abslistview.multichoicemodelistener; import android.widget.adapterview; import android.widget.adapterview.onitemclicklistener; import android.widget.arrayadapter; import android.widget.listadapter; import android.widget.listview; import android.widget.simplecursoradapter; import android.widget.spinner; import android.widget.textview; import android.widget.toast;  import com.google.android.gms.common.connectionresult; import com.google.android.gms.common.googleplayservicesutil;  import java.util.arraylist; import java.util.list;  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;      private settingsspinnerentrydialog dialogspinnersettings;      protected static allfilmsdatabase allfilmsdb;     protected static spinnersdatabase spinnersdb;     protected static singleshotdatabase singleshotdb;     protected static sqlitedatabase allfilmsdbase, spinnersdbase, singleshotdbase;      protected static cursor allfilmscursor, singleshotcursor;      protected static spinner addfilmcameraspinner, addfilmfilmspinner, addfilmisospinner, addfilmtotaltakesspinner;      @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.main_activity);          try {             allfilmsdb = new allfilmsdatabase(this);         }          catch (sqlexception allfilmsdatabaseexc) {             allfilmsdatabaseexc.printstacktrace();         }          try {             singleshotdb = new singleshotdatabase(this);         }         catch (sqlexception singlefilmdatabaseexc) {             singlefilmdatabaseexc.printstacktrace();         }          try {             spinnersdb = new spinnersdatabase(this);         }          catch (sqlexception spinnerexc) {             spinnerexc.printstacktrace();         }          mnavigationdrawerfragment = (navigationdrawerfragment) getfragmentmanager().findfragmentbyid(r.id.navigation_drawer);          mtitle = getstring(r.string.films_being_used);          // set drawer.         mnavigationdrawerfragment.setup(r.id.navigation_drawer, (drawerlayout) findviewbyid(r.id.drawer_layout));     }      @override      protected void onresume() {         super.onresume();          final int googleplayserviceavailability = googleplayservicesutil.isgoogleplayservicesavailable(this);          switch (googleplayserviceavailability) {             case connectionresult.success:                 log.i("googleplayserviceavailability", "success!");                 break;             case connectionresult.service_missing:                 googleplayservicesutil.geterrordialog(googleplayserviceavailability, this, 1).show();                 break;             case connectionresult.service_version_update_required:                 googleplayservicesutil.geterrordialog(googleplayserviceavailability, this, 2).show();                 break;             case connectionresult.service_disabled:                 googleplayservicesutil.geterrordialog(googleplayserviceavailability, this, 3).show();         }      }      @override     public void onnavigationdraweritemselected(int position) {         // update main content replacing fragments         fragmentmanager fragmentmanager =  getfragmentmanager();         switch(position) {         case 0:              fragmentmanager.begintransaction().replace(r.id.container, filmsbeingusedfragment.newinstance()).commit();             break;         case 1:             fragmentmanager.begintransaction().replace(r.id.container, addfilmfragment.newinstance()).commit();             break;         case 2:              fragmentmanager.begintransaction().replace(r.id.container, usedfilmfragment.newinstance()).commit();             break;         }      }      public void onsectionattached(int number) {         switch (number) {         case 1:             mtitle = getstring(r.string.films_being_used);             break;         case 2:             mtitle = getstring(r.string.add_film);             break;         case 3:             mtitle = getstring(r.string.used_films);             break;         case 4:             mtitle = getstring(r.string.settings);             break;         }     }      public void restoreactionbar() {         actionbar actionbar = getactionbar();         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_activity, 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();         return id == r.id.action_settings || super.onoptionsitemselected(item);     } } 

navigationdrawerfragment.java

package sc.erza.prancer;  import android.app.actionbar; import android.app.activity; import android.app.fragment; import android.content.sharedpreferences; import android.content.res.configuration; import android.os.bundle; import android.preference.preferencemanager; import android.support.v4.view.gravitycompat; import android.support.v4.widget.drawerlayout; import android.support.v7.app.actionbardrawertoggle; import android.view.layoutinflater; import android.view.menu; import android.view.menuinflater; import android.view.menuitem; import android.view.view; import android.view.viewgroup; import android.widget.adapterview; import android.widget.arrayadapter; import android.widget.listview;  /**  * fragment used managing interactions , presentation of navigation  * drawer. see <a href=  * "https://developer.android.com/design/patterns/navigation-drawer.html#interaction"  * > design guidelines</a> complete explanation of behaviors  * implemented here.  */ public class navigationdrawerfragment extends fragment {      /**      * remember position of selected item.      */     private static final string state_selected_position = "selected_navigation_drawer_position";      /**      * per design guidelines, should show drawer on launch until      * user manually expands it. shared preference tracks this.      */     private static final string pref_user_learned_drawer = "navigation_drawer_learned";      /**      * pointer current callbacks instance (the activity).      */     private navigationdrawercallbacks mcallbacks;      /**      * helper component ties action bar navigation drawer.      */     private actionbardrawertoggle mdrawertoggle;      private drawerlayout mdrawerlayout;     private listview mdrawerlistview;     private view mfragmentcontainerview;      private int mcurrentselectedposition = 0;     private boolean mfromsavedinstancestate;     private boolean muserlearneddrawer;      public navigationdrawerfragment() {     }      @override     public void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);          // read in flag indicating whether or not user has demonstrated         // awareness of         // drawer. see pref_user_learned_drawer details.         sharedpreferences sp = preferencemanager.getdefaultsharedpreferences(getactivity());         muserlearneddrawer = sp.getboolean(pref_user_learned_drawer, false);          if (savedinstancestate != null) {             mcurrentselectedposition = savedinstancestate                     .getint(state_selected_position);             mfromsavedinstancestate = true;         }          // select either default item (0) or last selected item.         selectitem(mcurrentselectedposition);     }      @override     public void onactivitycreated(bundle savedinstancestate) {         super.onactivitycreated(savedinstancestate);         // indicate fragment influence set of         // actions in action bar.         sethasoptionsmenu(true);     }      @override     public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) {         mdrawerlistview = (listview) inflater.inflate(r.layout.fragment_navigation_drawer, container, false);          mdrawerlistview.setonitemclicklistener(new adapterview.onitemclicklistener() {             @override             public void onitemclick(adapterview<?> parent, view view,                     int position, long id) {                 selectitem(position);             }         });          mdrawerlistview.setadapter(new arrayadapter<string>(getactionbar().getthemedcontext(),                 android.r.layout.simple_list_item_activated_1,                 android.r.id.text1, new string[]{                 getstring(r.string.films_being_used),                 getstring(r.string.add_film),                 getstring(r.string.used_films),                 getstring(r.string.settings),}));          mdrawerlistview.setitemchecked(mcurrentselectedposition, true);         return mdrawerlistview;     }      public boolean isdraweropen() {         return mdrawerlayout != null && mdrawerlayout.isdraweropen(mfragmentcontainerview);     }      /**      * users of fragment must call method set navigation      * drawer interactions.      *      * @param fragmentid      *            android:id of fragment in activity's layout.      * @param drawerlayout      *            drawerlayout containing fragment's ui.      */     public void setup(int fragmentid, drawerlayout drawerlayout) {         mfragmentcontainerview = getactivity().findviewbyid(fragmentid);         mdrawerlayout = drawerlayout;          // set custom shadow overlays main content when drawer         // opens         mdrawerlayout.setdrawershadow(r.drawable.drawer_shadow,                 gravitycompat.start);         // set drawer's list view items , click listener          actionbar actionbar = getactionbar();         actionbar.setdisplayhomeasupenabled(true);         actionbar.sethomebuttonenabled(true);          // actionbardrawertoggle ties the proper interactions         // between navigation drawer , action bar app icon.         mdrawertoggle = new actionbardrawertoggle(getactivity(), /* host activity */         mdrawerlayout, /* drawerlayout object */         r.string.navigation_drawer_open, /*                                          * "open drawer" description                                          * accessibility                                          */         r.string.navigation_drawer_close /*                                          * "close drawer" description                                          * accessibility                                          */         ) {             @override             public void ondrawerclosed(view drawerview) {                 super.ondrawerclosed(drawerview);                 if (!isadded()) {                     return;                 }                   getactivity().invalidateoptionsmenu();  // calls                                                         // onprepareoptionsmenu()             }              @override             public void ondraweropened(view drawerview) {                 super.ondraweropened(drawerview);                 if (!isadded()) {                     return;                 }                  if (!muserlearneddrawer) {                     // user manually opened drawer; store flag                     // prevent auto-showing                     // navigation drawer automatically in future.                     muserlearneddrawer = true;                     sharedpreferences sp = preferencemanager.getdefaultsharedpreferences(getactivity());                     sp.edit().putboolean(pref_user_learned_drawer, true).apply();                 }                  getactivity().invalidateoptionsmenu();  // calls                                                         // onprepareoptionsmenu()             }         };          // if user hasn't 'learned' drawer, open introduce         // them drawer,         // per navigation drawer design guidelines.         if (!muserlearneddrawer && !mfromsavedinstancestate) {             mdrawerlayout.opendrawer(mfragmentcontainerview);         }          // defer code dependent on restoration of previous instance state.         mdrawerlayout.post(new runnable() {             @override             public void run() {                 mdrawertoggle.syncstate();             }         });          mdrawerlayout.setdrawerlistener(mdrawertoggle);     }      private void selectitem(int position) {         mcurrentselectedposition = position;         if (mdrawerlistview != null) {             mdrawerlistview.setitemchecked(position, true);         }         if (mdrawerlayout != null) {             mdrawerlayout.closedrawer(mfragmentcontainerview);         }         if (mcallbacks != null) {             mcallbacks.onnavigationdraweritemselected(position);         }     }      @override     public void onattach(activity activity) {         super.onattach(activity);         try {             mcallbacks = (navigationdrawercallbacks) activity;         } catch (classcastexception e) {             throw new classcastexception(                     "activity must implement navigationdrawercallbacks.");         }     }      @override     public void ondetach() {         super.ondetach();         mcallbacks = null;     }      @override     public void onsaveinstancestate(bundle outstate) {         super.onsaveinstancestate(outstate);         outstate.putint(state_selected_position, mcurrentselectedposition);     }      @override     public void onconfigurationchanged(configuration newconfig) {         super.onconfigurationchanged(newconfig);         // forward new configuration drawer toggle component.         mdrawertoggle.onconfigurationchanged(newconfig);     }      @override     public void oncreateoptionsmenu(menu menu, menuinflater inflater) {         // if drawer open, show global app actions in action bar.         // see         // showglobalcontextactionbar, controls top-left area of         // action bar.         if (mdrawerlayout != null && isdraweropen()) {             inflater.inflate(r.menu.global, menu);             showglobalcontextactionbar();         }         super.oncreateoptionsmenu(menu, inflater);     }      @override     public boolean onoptionsitemselected(menuitem item) {         return mdrawertoggle.onoptionsitemselected(item) || super.onoptionsitemselected(item);      }      /**      * per navigation drawer design guidelines, updates action bar      * show global app 'context', rather what's in current      * screen.      */     private void showglobalcontextactionbar() {         actionbar actionbar = getactionbar();         actionbar.setdisplayshowtitleenabled(true);         actionbar.settitle("");     }      private actionbar getactionbar() {         return getactivity().getactionbar();     }      /**      * callbacks interface activities using fragment must      * implement.      */     public static interface navigationdrawercallbacks {         /**          * called when item in navigation drawer selected.          */         void onnavigationdraweritemselected(int position);     } } 

main_activity.xml

<!-- drawerlayout intended used top-level content view using match_parent both width , height consume full space available. --> <android.support.v4.widget.drawerlayout xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:tools="http://schemas.android.com/tools"     android:id="@+id/drawer_layout"     android:layout_width="match_parent"     android:layout_height="match_parent"     tools:context="sc.erza.prancer.mainactivity" >      <!--          main content view, view below consumes entire          space available using match_parent in both dimensions.     -->      <framelayout         android:id="@+id/container"         android:layout_width="match_parent"         android:layout_height="match_parent" >      </framelayout>      <!--          android:layout_gravity="start" tells drawerlayout treat          sliding drawer on left side left-to-right          languages , on right side right-to-left languages.          if you're not building against api 17 or higher, use          android:layout_gravity="left" instead.     -->     <!--          drawer given fixed width in dp , extends full height of          container.     -->      <fragment         android:id="@+id/navigation_drawer"         android:name="sc.erza.prancer.navigationdrawerfragment"         android:layout_width="@dimen/navigation_drawer_width"         android:layout_height="match_parent"         android:layout_gravity="start"         tools:layout="@layout/fragment_navigation_drawer" />  </android.support.v4.widget.drawerlayout> 

getactivity().getapplicationcontext(); instead of getactionbar().getthemedcontext();


Comments

Popular posts from this blog

java - Plugin org.apache.maven.plugins:maven-install-plugin:2.4 or one of its dependencies could not be resolved -

Round ImageView Android -

How can I utilize Yahoo Weather API in android -