java - Null pointer exception DrawerLayout -


here mainactivity.java

import android.content.context; import android.content.sharedpreferences; import android.support.v4.widget.drawerlayout; import android.support.v7.app.actionbaractivity; import android.os.bundle; import android.support.v7.app.actionbardrawertoggle; 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.arrayadapter; import android.widget.listview;   public class mainactivity extends appcompatactivity {  listview mnavigationdrawer;   private toolbar mtoolbar; private actionbardrawertoggle mdrawertoggle;  drawerlayout mdrawerlayout;  private boolean muserlearneddrawer; private boolean mfromsavedinstancestate;  public static final string pref_file_name = "pref"; public static final string key_user_learned_drawer = "user_learned_drawer";  @override protected void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.activity_main);     if (savedinstancestate != null) {         mfromsavedinstancestate = true;     }     mtoolbar = (toolbar) findviewbyid(r.id.app_bar);     setsupportactionbar(mtoolbar);      getsupportactionbar().setdisplayhomeasupenabled(true);      mdrawerlayout = (drawerlayout) findviewbyid(r.id.main_drawerlayout);      mnavigationdrawer = (listview) findviewbyid(r.id.navigation_drawer);     mnavigationdrawer.setadapter(new arrayadapter<>(this, android.r.layout.simple_list_item_1, getresources().getstringarray(r.array.navigation_drawer_list)));      setdrawers(mdrawerlayout, mtoolbar); }  @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); } public void setdrawers(drawerlayout drawerlayout, toolbar toolbar) {     mdrawerlayout = drawerlayout;     mtoolbar = toolbar;     mdrawertoggle = new actionbardrawertoggle(this, drawerlayout, toolbar, r.string.navigation_drawer_open, r.string.navigation_drawer_close) {         @override         public void ondraweropened(view drawerview) {             super.ondraweropened(drawerview);             if (!muserlearneddrawer)                 muserlearneddrawer = true;             savetopreference(getapplicationcontext(), key_user_learned_drawer, muserlearneddrawer + ""); //                getactivity().invalidateoptionsmenu();          }          @override         public void ondrawerclosed(view drawerview) {             super.ondrawerclosed(drawerview); //                getactivity().invalidateoptionsmenu();         }          @override         public void ondrawerslide(view drawerview, float slideoffset) {             super.ondrawerslide(drawerview, slideoffset);         }     };     mdrawerlayout.setdrawerlistener(mdrawertoggle);     if (!muserlearneddrawer && !mfromsavedinstancestate)         mdrawerlayout.opendrawer(mnavigationdrawer);      mdrawerlayout.post(new runnable() {         @override         public void run() {             mdrawertoggle.syncstate();         }     }); }  public static void savetopreference(context context, string preferencename, string preferencevalue) {     sharedpreferences sharedpreferences = context.getsharedpreferences(pref_file_name, context.mode_private);     sharedpreferences.editor editor = sharedpreferences.edit();     editor.putstring(preferencename, preferencevalue);     editor.apply(); //        editor.commit() }  public static string readfrompreference(context context, string preferencename, string defaultvalue) {     sharedpreferences sharedpreferences = context.getsharedpreferences(pref_file_name, context.mode_private);     return sharedpreferences.getstring(preferencename, defaultvalue); } } 

here activity_main.xml

<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical">  <include     android:id="@+id/app_bar"     layout="@layout/app_bar" />  <android.support.v4.widget.drawerlayout     android:name="@+id/main_drawerlayout"     android:layout_width="match_parent"     android:layout_height="match_parent">      <relativelayout         android:layout_width="match_parent"         android:layout_height="match_parent"         tools:context="xymen.chetanbhagatnovels.mainactivity">          <textview             android:layout_width="wrap_content"             android:layout_height="wrap_content"             android:text="@string/hello_world" />      </relativelayout>      <listview         android:id="@+id/navigation_drawer"         android:layout_width="@dimen/navigation_drawer_width"         android:layout_height="match_parent"         android:layout_gravity="start"         android:divider="@null"/>  </android.support.v4.widget.drawerlayout> </linearlayout> 

it giving me error on line mdrawerlayout.setdrawerlistener(mdrawertoggle); dont know why. have project have copied nor getting error 1 does. help

i ran code , figured out.

the problem have specified name instead of id android.support.v4.widget.drawerlayout.

just change this:

android:name="@+id/main_drawerlayout" 

to this:

android:id="@+id/main_drawerlayout" 

with change, nullpointerexception fixed , ran fine me.


Comments

Popular posts from this blog

qt - Using float or double for own QML classes -

Create Outlook appointment via C# .Net -

ios - Swift Array Resetting Itself -