android - PreferenceActivity with Navigation Drawer Throws Null Pointer Exception -


i had preferenceactivity working fine. then, decided add navigation drawer it, , edited layouts according other activity layouts have working navigation drawers. however, after doing that, preferenceactivity stopped opening , started throwing npe on start. can't open activity anymore. problem in oncreate, obviously, , in onclick method stated in logcat. yet, there 2 onclick's in activities oncreate, , i'm not seeing how result in npe, since exact same in other working activities. layout xmls seperated 3 xmls according default drawerlayout implementation. without further due, here codes , xmls:

preferenceactivity:

public class firstyearcoursesactivity extends preferenceactivity     implements navigationview.onnavigationitemselectedlistener {  private appcompatdelegate mdelegate; toolbar toolbar;  @override protected void oncreate(bundle savedinstancestate) {     getdelegate().installviewfactory();     getdelegate().oncreate(savedinstancestate);     super.oncreate(savedinstancestate);     setcontentview(r.layout.activity_first_year_courses);     toolbar = (toolbar) findviewbyid(r.id.toolbar);     setsupportactionbar((toolbar));     window window = this.getwindow();     window.clearflags(windowmanager.layoutparams.flag_translucent_status);     window.addflags(windowmanager.layoutparams.flag_draws_system_bar_backgrounds);     window.setstatusbarcolor(this.getresources().getcolor(r.color.colorprimarydark));     addpreferencesfromresource(r.xml.prefs);     drawerlayout drawer = (drawerlayout) findviewbyid(r.id.drawer_layout);     actionbardrawertoggle toggle = new actionbardrawertoggle(             this, drawer, toolbar, r.string.navigation_drawer_open, r.string.navigation_drawer_close);     drawer.setdrawerlistener(toggle);     toggle.syncstate();      navigationview navigationview = (navigationview) findviewbyid(r.id.nav_view2);     navigationview.setnavigationitemselectedlistener(this);     view headerview = navigationview.getheaderview(0);     imagebutton logoutbutton = (imagebutton) headerview.findviewbyid(r.id.logoutbutton);     logoutbutton.setonclicklistener(new view.onclicklistener() {         @override         public void onclick(view v) {             intent intent = new intent(firstyearcoursesactivity.this, loginactivity.class);             startactivity(intent);         }     }); }  @override public void onbackpressed() {     drawerlayout drawer = (drawerlayout) findviewbyid(r.id.drawer_layout);     if (drawer.isdraweropen(gravitycompat.start)) {         drawer.closedrawer(gravitycompat.start);     } else {         super.onbackpressed();     } }  @override protected void onpause() {     final drawerlayout drawer = (drawerlayout) findviewbyid(r.id.drawer_layout);     final handler handler = new handler();     if (drawer.isdraweropen(gravitycompat.start)) {         handler.postdelayed(new runnable() {             @override             public void run() {                 drawer.closedrawer(gravitycompat.start);             }         }, 200);         super.onpause();     } else {         super.onpause();     } } private void setsupportactionbar(@nullable toolbar toolbar) {     getdelegate().setsupportactionbar(toolbar); }  private appcompatdelegate getdelegate() {     if (mdelegate == null) {         mdelegate = appcompatdelegate.create(this, null);     }     return mdelegate; }  @override public boolean onpreferencetreeclick(preferencescreen preferencescreen, preference preference) {      string key = preference.getkey();         switch (key) {             case "math119":                 intent intent = new intent(firstyearcoursesactivity.this, math119activity.class);                 startactivity(intent);                 break;             default:                 toast.maketext(this, "other click", toast.length_short).show();                 break;         }      return super.onpreferencetreeclick(preferencescreen, preference); }  @suppresswarnings("statementwithemptybody") @override public boolean onnavigationitemselected(menuitem item) {    // filled     int id = item.getitemid();      if (id == r.id.nav_camera) {      } else if (id == r.id.nav_gallery) {      } else if (id == r.id.nav_slideshow) {      } else if (id == r.id.nav_manage) {      } else if (id == r.id.nav_share) {      } else if (id == r.id.nav_send) {      }     return true; } } 

activity.xml:

<android.support.v4.widget.drawerlayout android:id="@+id/drawer_layout" xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" android:fitssystemwindows="true">  <include     layout="@layout/app_bar_first_year_courses"     android:layout_width="match_parent"     android:layout_height="match_parent"/>  <android.support.design.widget.navigationview     android:id="@+id/nav_view2"     android:layout_width="250dp"     android:layout_height="match_parent"     android:layout_gravity="start"     android:fitssystemwindows="true"     app:headerlayout="@layout/nav_header_main"     app:menu="@menu/activity_main_drawer"     android:background="#ffffffff"     app:itemtextcolor="#000000"     app:itemtextappearance="@style/textappearance.appcompat.body1"/> </android.support.v4.widget.drawerlayout> 

app_bar.xml:

<android.support.design.widget.coordinatorlayout 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:fitssystemwindows="true" tools:context=".activities.firstyearcoursesactivity">  <android.support.design.widget.appbarlayout     android:layout_width="match_parent"     android:layout_height="wrap_content"     android:theme="@style/apptheme.appbaroverlay">      <android.support.v7.widget.toolbar         android:id="@+id/toolbar"         android:layout_width="match_parent"         android:layout_height="?attr/actionbarsize"         android:background="?attr/colorprimary"         app:popuptheme="@style/apptheme.popupoverlay"/>  </android.support.design.widget.appbarlayout>  <include layout="@layout/content_first_year_courses"/>  </android.support.design.widget.coordinatorlayout> 

content.xml:

<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" app:layout_behavior="@string/appbar_scrolling_view_behavior" tools:context=".activities.firstyearcoursesactivity" tools:showin="@layout/app_bar_first_year_courses" android:orientation="vertical">  </linearlayout> 

nav_header.xml:

<relativelayout xmlns:android="http://schemas.android.com/apk/res/android"           android:layout_width="match_parent"           android:layout_height="140dp"           android:background="@drawable/profilebanner"           android:gravity="bottom"           android:orientation="vertical"           android:theme="@style/themeoverlay.appcompat.dark"           android:id="@+id/header">  <imagebutton     android:layout_width="wrap_content"     android:layout_height="wrap_content"     android:id="@+id/logoutbutton"     android:src="@drawable/ic_power_settings_new_white_18dp"     android:background="#00ffffff"     android:layout_alignparenttop="true"     android:layout_alignparentend="true"     android:layout_margintop="30dp"     android:layout_marginright="10dp"/>  <relativelayout     android:layout_width="match_parent"     android:layout_height="match_parent"     android:id="@+id/headerclick"     android:layout_alignparentbottom="true"     android:layout_alignparentstart="true"     android:paddingleft="10dp"     android:paddingright="10dp"     android:paddingbottom="10dp"     android:paddingtop="20dp">      <imageview         android:layout_width="65dp"         android:layout_height="65dp"         android:id="@+id/imageview"         android:background="@drawable/onur"         android:layout_centervertical="true"         android:layout_alignparentstart="false"         android:layout_alignbottom="@+id/textview3"         android:layout_marginbottom="-20dp"/>      <textview         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:text="onur Çevik"         android:textappearance="@style/textappearance.appcompat.body1"         android:id="@+id/textview2"         android:textsize="13sp"         android:layout_torightof="@+id/imageview"         android:layout_marginleft="25dp"         android:layout_margintop="45dp"/>      <textview         android:id="@+id/textview3"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:text="environmental engineering"         android:textsize="11sp"         android:layout_torightof="@+id/imageview"         android:layout_marginleft="25dp"         android:layout_below="@+id/textview2"         android:layout_alignparentstart="false"/>  </relativelayout>  </relativelayout> 

prefs.xml:

<preferencescreen xmlns:android="http://schemas.android.com/apk/res/android" android:key="firstyearprefs"> <preference     android:title="math-119"     android:key="math119"/> <preference     android:title="math-120"     android:key="math120"/> <preference     android:title="phys-105"     android:key="phys105"/> <preference     android:title="phys-106"     android:key="phys106"/> <preference     android:title="chem-107"     android:key="chem107"/> <preference     android:title="eng-101"     android:key="eng101"/> <preference     android:title="eng-102"     android:key="eng102"/> <preference     android:title="ce-101"     android:key="ce101"/>  </preferencescreen> 

logcat:

02-08 02:10:35.415 30815-30815/com.theoc.proto e/androidruntime: fatal exception: main                                                              process: com.theoc.proto, pid: 30815                                                              java.lang.nullpointerexception: attempt invoke virtual method 'void android.widget.listview.setonitemclicklistener(android.widget.adapterview$onitemclicklistener)' on null object reference                                                                  @ android.preference.preferencescreen.bind(preferencescreen.java:143)                                                                  @ android.preference.preferenceactivity.bindpreferences(preferenceactivity.java:1419)                                                                  @ android.preference.preferenceactivity.access$000(preferenceactivity.java:123)                                                                  @ android.preference.preferenceactivity$1.handlemessage(preferenceactivity.java:230)                                                                  @ android.os.handler.dispatchmessage(handler.java:102)                                                                  @ android.os.looper.loop(looper.java:135)                                                                  @ android.app.activitythread.main(activitythread.java:5272)                                                                  @ java.lang.reflect.method.invoke(native method)                                                                  @ java.lang.reflect.method.invoke(method.java:372)                                                                  @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:909)                                                                  @ com.android.internal.os.zygoteinit.main(zygoteinit.java:704) 

edit:

one thing worth mentioning triple checked activity , it's adapter leads preferenceactivity on list item click. reversed drawer layout implementations, , able open preferenceactivity through parent activity. navigation drawer code, or layouts ones messing it. i'm getting listview.onclick(adapter) npe because list item click on parent activity goes null reference, in case preferenceactivity itself. debug didn't show me null in preferenceactivity's oncreate, except savedinstancestate.

i able reproduce same npe using code , fixed replacing addpreferencesfromresource call (note it's deprecated) using fragment instead described in these threads:

android: preference fragment navigation drawer's fragment

what use instead of "addpreferencesfromresource" in preferenceactivity?

although, on version, preferences view overlaps toolbar , not sure activity layout should fix that, maybe ok in yours.


Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -