java - Null Exception in Navigation Drawer -


i applying navigation drawer in project. have add 3 items in list of navigation drawer fb_fragment, tb_fragment, g_fragment. when click of these items respective section open. problem faced when run project give me error

" java.lang.runtimeexception: unable start activity componentinfo{com.example.abdul.navigation/com.example.abdul.navigation.mainactivity}: java.lang.nullpointerexception: attempt invoke virtual method 'void android.app.actionbar.setdisplayhomeasupenabled(boolean)' on null object reference "   "caused by: java.lang.nullpointerexception: attempt invoke virtual method 'void android.app.actionbar.setdisplayhomeasupenabled(boolean)' on null object reference " 

mainactivity.java

package com.example.abdul.navigation;  import java.util.arraylist; import java.util.list; import android.annotation.suppresslint; import android.app.actionbar; import android.app.activity; import android.app.fragment; import android.app.fragmentmanager; import android.content.res.configuration; import android.content.res.typedarray; import android.os.bundle; import android.support.v4.app.actionbardrawertoggle; import android.support.v4.app.activitycompat; import android.support.v4.widget.drawerlayout; import android.support.v7.app.actionbaractivity; import android.util.log; import android.view.menu; import android.view.menuitem; import android.view.view; import android.widget.adapterview; import android.widget.arrayadapter; import com.example.abdul.navigation.fb_fragment; import android.widget.listview; import com.example.abdul.navigation.g_fragment; import com.example.abdul.navigation.tb_fragment;  public class mainactivity extends actionbaractivity { string[] menutitles;  typedarray menuicons; // nav drawer title private charsequence mdrawertitle; private charsequence mtitle; private drawerlayout mdrawerlayout; private listview mdrawerlist; private actionbardrawertoggle mdrawertoggle; private list<rowitem> rowitems; private customadapter adapter;  @suppresslint("newapi") @override  protected void oncreate(bundle savedinstancestate) {      setcontentview(r.layout.activity_main);      mtitle = mdrawertitle = gettitle();     menutitles = getresources().getstringarray(r.array.titles);     menuicons = getresources().obtaintypedarray(r.array.icons);     mdrawerlayout = (drawerlayout) findviewbyid(r.id.drawer_layout);     mdrawerlist = (listview) findviewbyid(r.id.slider_list);      rowitems = new arraylist<rowitem>();      (int = 0; < menutitles.length; i++) {         rowitem items = new rowitem(menutitles[i], menuicons.getresourceid(      i, -1));         rowitems.add(items);     }      menuicons.recycle();     adapter = new customadapter(getapplicationcontext(), rowitems);     mdrawerlist.setadapter(adapter);     mdrawerlist.setonitemclicklistener(new slideitemlistener());      // enabling action bar app icon , behaving toggle button     getsupportactionbar().setdisplayhomeasupenabled(true);     getsupportactionbar().sethomebuttonenabled(true);      mdrawertoggle = new actionbardrawertoggle(this, mdrawerlayout,r.drawable.ic_drawer, r.string.app_name,r.string.app_name)     {         public void ondrawerclosed(view view) {             getsupportactionbar().settitle(mtitle);             // calling onprepareoptionsmenu() show action bar icons             activitycompat.invalidateoptionsmenu(getparent());         }         public void ondraweropened(view drawerview) {             getsupportactionbar().settitle(mdrawertitle);             // calling onprepareoptionsmenu() hide action bar icons             activitycompat.invalidateoptionsmenu(getparent());         }     };     mdrawerlayout.setdrawerlistener(mdrawertoggle);     if (savedinstancestate == null) {         // on first time display view first nav item         updatedisplay(0);     } }     class slideitemlistener implements listview.onitemclicklistener {      @override     public void onitemclick(adapterview<?> parent, view view, int position, long id)     {                  updatedisplay(position);     }  }  private void updatedisplay(int position) {     fragment fragment = null;     switch (position) {         case 0:      fragment = new fb_fragment();             break;         case 1:       fragment = new g_fragment();             break;         case 2:         fragment = new tb_fragment();             break;         default:             break;     }     if (fragment != null) {         fragmentmanager fragmentmanager = getfragmentmanager();         fragmentmanager.begintransaction().replace(r.id.frame_container, fragment).commit();         // update selected item , title, close drawer         settitle(menutitles[position]);         mdrawerlayout.closedrawer(mdrawerlist);     }     else {         // error in creating fragment         log.e("mainactivity", "error in creating fragment");     } } @override public void settitle(charsequence title) {     mtitle = title;     getsupportactionbar().settitle(mtitle); }  @override public boolean onoptionsitemselected(menuitem item) {     // toggle nav drawer on selecting action bar app icon/title     if (mdrawertoggle.onoptionsitemselected(item)) {         return true;     }     // handle action bar actions click     switch (item.getitemid())     {         case          r.id.action_settings:         return true;         default:             return super.onoptionsitemselected(item);     } }  /***   * called when invalidateoptionsmenu() triggered   */ @override      public boolean onprepareoptionsmenu(menu menu) {     // if nav drawer opened, hide action items     boolean draweropen = mdrawerlayout.isdraweropen(mdrawerlist);     menu.finditem(r.id.action_settings).setvisible(!draweropen);     return super.onprepareoptionsmenu(menu); }  /**   * when using actionbardrawertoggle, must call during   * onpostcreate() , onconfigurationchanged()...   */ @override protected void onpostcreate(bundle savedinstancestate) {     super.onpostcreate(savedinstancestate);     // sync toggle state after onrestoreinstancestate has occurred.     mdrawertoggle.syncstate();    } @override public void onconfigurationchanged(configuration newconfig) {     super.onconfigurationchanged(newconfig);     // pass configuration change drawer toggles     mdrawertoggle.onconfigurationchanged(newconfig); } 

}

customadapter.java

package com.example.abdul.navigation; import java.util.list; import android.app.activity; import android.content.context; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; import android.widget.baseadapter; import android.widget.imageview; import android.widget.textview; public class customadapter extends baseadapter { context context;  list<rowitem> rowitem;  customadapter(context context, list<rowitem> rowitem) {     this.context = context;     this.rowitem = rowitem; }  @override public view getview(int position, view convertview, viewgroup parent) {     if (convertview == null) {         layoutinflater minflater = (layoutinflater) context                     .getsystemservice(activity.layout_inflater_service);         convertview = minflater.inflate(r.layout.list_tem, null);     }     imageview imgicon = (imageview) convertview.findviewbyid(r.id.icon);     textview txttitle = (textview) convertview.findviewbyid(r.id.title);     rowitem row_pos = rowitem.get(position);      // setting image resource , title     imgicon.setimageresource(row_pos.geticon());     txttitle.settext(row_pos.gettitle());     return convertview; }  @override public int getcount() {     return rowitem.size(); }  @override public object getitem(int position) {     return rowitem.get(position); }  @override public long getitemid(int position) {     return rowitem.indexof(getitem(position)); } } 

rowitem.java

package com.example.abdul.navigation; public class rowitem { private string title; private int icon; public rowitem(string title, int icon) {     this.title = title;     this.icon = icon; } public string gettitle() {     return title; } public void settitle(string title) {     this.title = title; } public int geticon() {     return icon; } public void seticon(int icon) {     this.icon = icon; } } 

fb_fragment.java

package com.example.abdul.navigation; import android.annotation.suppresslint; import android.app.fragment; import com.example.abdul.navigation.r; import android.os.bundle; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; @suppresslint("newapi") public class fb_fragment extends fragment {  @override public view oncreateview(layoutinflater inflater, viewgroup     container, bundle savedinstancestate) {     view rootview = inflater .inflate(r.layout.fb_fragment, container, false);     return rootview; }  } 

tb_fragment.java

package com.example.abdul.navigation; import android.annotation.suppresslint; import android.app.fragment; import com.example.abdul.navigation.r; import android.os.bundle; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; @suppresslint("newapi") public class tb_fragment extends fragment {      @override     public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) {         view rootview = inflater.inflate(r.layout.tb_fragment, container, false);         return rootview;     } } 

g_fragment.java

package com.example.abdul.navigation; import android.annotation.suppresslint; import android.app.fragment; import com.example.abdul.navigation.r; import android.os.bundle; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; @suppresslint("newapi") public class g_fragment extends fragment {  @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) {     view rootview = inflater .inflate(r.layout.g_fragment, container, false);     return rootview; } } 

activity_main.xml

<android.support.v4.widget.drawerlayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/drawer_layout" android:layout_width="match_parent" android:layout_height="match_parent" > <!-- main content view --> <framelayout     android:id="@+id/frame_container"     android:layout_width="match_parent"     android:layout_height="match_parent" /> <!-- navigation drawer list --> <listview     android:id="@+id/slider_list"     android:layout_width="240dp"     android:layout_height="match_parent"     android:layout_gravity="start"     android:background="#ffffff"     android:choicemode="singlechoice"     android:divider="@android:color/transparent"     android:dividerheight="0dp" /> </android.support.v4.widget.drawerlayout> 

fb_fragment.xml

<?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <imageview     android:id="@+id/imageview1"     android:layout_width="100dp"     android:layout_height="100dp"     android:layout_alignparenttop="true"     android:layout_centerhorizontal="true"     android:layout_margintop="148dp"     android:src="@drawable/fb" /> </relativelayout> 

g_fragment.xml

<?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <imageview     android:id="@+id/imageview1"     android:layout_width="100dp"     android:layout_height="100dp"     android:layout_alignparenttop="true"     android:layout_centerhorizontal="true"     android:layout_margintop="148dp"     android:src="@drawable/gplus" /> </relativelayout> 

list_tem.xml

<?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="48dp" android:padding="5dp" > <imageview     android:id="@+id/icon"     android:layout_width="50dp"     android:layout_height="wrap_content"     android:layout_alignparentleft="true"     android:layout_centervertical="true"     android:layout_marginleft="12dp"     android:layout_marginright="12dp"     android:contentdescription="@string/imgdesc" /> <textview     android:id="@+id/title"     android:layout_width="wrap_content"     android:layout_height="match_parent"     android:layout_margintop="10dp"     android:layout_torightof="@id/icon"     android:gravity="center_vertical"     android:textcolor="#000000"     android:textsize="20sp" /> </relativelayout> 

tb_fragment.xml

<?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <imageview     android:id="@+id/imageview1"     android:layout_width="100dp"     android:layout_height="100dp"     android:layout_alignparenttop="true"     android:layout_centerhorizontal="true"     android:layout_margintop="148dp"     android:src="@drawable/tb" /> </relativelayout> 

strings.xml

<?xml version="1.0" encoding="utf-8"?> <resources>  <string name="app_name">navigation</string> <string name="hello_world">hello world!</string> <string name="action_settings">settings</string> <string name="imgdesc">imgdesc</string> <string-array name="titles">     <item>facebook</item>     <item>google-plus</item>     <item>tutorialsbuzz</item> </string-array> <array name="icons">     <item>@drawable/fb</item>     <item>@drawable/gplus</item>     <item>@drawable/tb</item> </array> </resources> 

styles.xml

<resources>  <!-- base application theme. --> <style name="apptheme" parent="theme.appcompat.light.darkactionbar">     <!-- customize theme here. --> </style>  </resources> 

in support libraries actionbar use

getsupportactionbar(); 

you using getactionbar() giving null.

so replace getactionbar() getsupportactionbar().

update:

delete styles.xml , write following in styles.xml

<!--     base application theme, dependent on api level. theme replaced     appbasetheme res/values-vxx/styles.xml on newer devices. --> <style name="appbasetheme" parent="theme.appcompat.light">     <!--         theme customizations available in newer api levels can go in         res/values-vxx/styles.xml, while customizations related         backward-compatibility can go here.     --> </style>  <!-- application theme. --> <style name="apptheme" parent="appbasetheme">     <!-- customizations not specific particular api-level can go here. --> </style> 

hope helps you.


Comments

Popular posts from this blog

firemonkey - How do I make a beep sound in Android using Delphi and the API? -

twig - Using Twigbridge in a Laravel 5.1 Package -

jdbc - Not able to establish database connection in eclipse -