java - Error in Navigation drawer/error with Nullpointer exception -
hello programmed quiz 3 activitys: 1 = quizactivity, 2= menu2 , 3= menu3. activity 1 , 2 working fine. problem in menu3. there 1 code of line creating nullpoiner excption: void android.support.v7.app.actionbar.setdisplayhomeasupenabled(boolean)' on null object reference when delete line can start (by clicking item on navigation drawer(leads menu3 activity)) menu3 activity. problem when leave out line "burger icon" (you can see on picture) not there anymore. when leave code of line can`t navigate menu3 because when app force closes. how can solve problem?
menu3 java:
package amapps.impossiblequiz; import static amapps.impossiblequiz.r.id.nv3; public class menu3 extends appcompatactivity { private drawerlayout mdrawerlayout3; private actionbardrawertoggle mtoggle; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_menu3); toolbar mtoolbar = (toolbar) findviewbyid(r.id.nav_action); setsupportactionbar(mtoolbar); mdrawerlayout3 = (drawerlayout) findviewbyid(r.id.drawerlayout3); mtoggle = new actionbardrawertoggle(this, mdrawerlayout3, r.string.open, r.string.close); mdrawerlayout3.adddrawerlistener(mtoggle); mtoggle.syncstate(); getsupportactionbar().setdisplayhomeasupenabled(true); navigationview mnavigationview = (navigationview) findviewbyid(nv3); mnavigationview.setnavigationitemselectedlistener(new navigationview.onnavigationitemselectedlistener() { @override public boolean onnavigationitemselected(menuitem menuitem){ switch (menuitem.getitemid()){ case(r.id.nav_home3): intent accountactivity3 = new intent(getapplicationcontext(),quizactivity.class); startactivity(accountactivity3); } return true; } }); } @override //makes "burger" item, shows drawer if clicks on simbol public boolean onoptionsitemselected(menuitem item) { if (mtoggle.onoptionsitemselected(item)) { return true; } return super.onoptionsitemselected(item); }
}
menu3 xml:
<?xml version="1.0" encoding="utf-8"?> <android.support.v4.widget.drawerlayout 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" tools:context="amapps.impossiblequiz.menu3" android:id="@+id/drawerlayout3"> <relativelayout android:layout_width="match_parent" android:layout_height="match_parent"> <button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignparentbottom="true" android:layout_alignparentend="true" android:layout_alignparentright="true" android:layout_marginbottom="215dp" android:layout_marginend="42dp" android:layout_marginright="42dp" android:text="button" /> </relativelayout> <android.support.design.widget.navigationview android:layout_width="wrap_content" android:layout_height="match_parent" app:menu="@menu/navigation_menu3" android:layout_gravity="start" android:id="@+id/nv3" app:headerlayout="@layout/navigation_header" app:itemicontint="@drawable/tint_color_selector2"> </android.support.design.widget.navigationview> </android.support.v4.widget.drawerlayout>
menu2 xml:
<?xml version="1.0" encoding="utf-8"?> <android.support.v4.widget.drawerlayout 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" tools:context="amapps.impossiblequiz.menu2" android:id="@+id/drawerlayout2" android:orientation="vertical"> <linearlayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <include layout="@layout/navigation_action" android:layout_width="match_parent" android:layout_height="wrap_content" /> <relativelayout android:layout_width="wrap_content" android:layout_height="match_parent"> <textview android:id="@+id/textscore2" android:layout_width="wrap_content" android:layout_height="50dp" android:background="#f60" android:paddingtop="10dp" android:paddingleft="35dp" android:text="your score is:" android:textappearance="@style/textappearance.appcompat" android:textcolor="#ffffff" android:textsize="20dp" android:layout_margintop="28dp" android:layout_below="@+id/textview2" android:layout_alignparentleft="true" android:layout_alignparentstart="true" android:layout_alignparentright="true" android:layout_alignparentend="true" /> <textview android:id="@+id/texthighscore" android:layout_width="wrap_content" android:layout_height="50dp" android:background="#ffab00" android:paddingleft="35dp" android:paddingright="100dp" android:paddingtop="10dp" android:text="highest score:" android:textappearance="@style/textappearance.appcompat" android:textcolor="#ffffff" android:textsize="20dp" android:layout_margintop="13dp" android:layout_below="@+id/textscore2" android:layout_alignparentleft="true" android:layout_alignparentstart="true" android:layout_alignparentright="true" android:layout_alignparentend="true" /> <button android:id="@+id/tryagain_button" android:layout_width="150dp" android:layout_height="50dp" android:background="@drawable/bg_pill" android:onclick="onclick" android:text="restart quiz!" android:textcolor="#ffffff" android:layout_margintop="18dp" android:layout_below="@+id/texthighscore" android:layout_centerhorizontal="true" /> <textview android:id="@+id/textview2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margintop="33dp" android:text="stats: play , trophies!" android:textappearance="@style/textappearance.appcompat.button" android:textsize="20dp" android:layout_alignparenttop="true" android:layout_centerhorizontal="true" /> <imageview android:id="@+id/statstrophy" app:srccompat="@drawable/statsoftrophy" android:layout_width="50dp" android:layout_height="50dp" android:layout_aligntop="@+id/texthighscore" android:layout_above="@+id/tryagain_button" android:layout_alignparentright="true" android:layout_alignparentend="true" /> <imageview android:id="@+id/trophy1" android:layout_width="80dp" android:layout_height="80dp" android:layout_alignparentleft="true" android:layout_alignparentstart="true" android:layout_below="@+id/tryagain_button" android:layout_marginleft="26dp" android:layout_marginstart="26dp" android:layout_margintop="70dp" android:visibility="invisible" app:srccompat="@drawable/trophy1" /> <imageview android:id="@+id/trophy2" android:layout_width="80dp" android:layout_height="80dp" android:layout_aligntop="@+id/trophy1" android:layout_marginleft="24dp" android:layout_marginstart="24dp" android:layout_toendof="@+id/trophy1" android:layout_torightof="@+id/trophy1" android:visibility="invisible" app:srccompat="@drawable/trophy2" /> <button android:id="@+id/enablepopup" android:layout_width="80dp" android:layout_height="30dp" android:text="info" android:alpha="0.30" android:visibility="invisible" android:background="@drawable/bg_pill_trophy" android:textcolor="#ffffff" android:layout_aligntop="@+id/enablepopup2" android:layout_alignleft="@+id/trophy1" android:layout_alignstart="@+id/trophy1" /> <button android:id="@+id/enablepopup2" android:layout_width="60dp" android:layout_height="30dp" android:background="@drawable/bg_pill_trophy" android:text="info" android:textcolor="#ffffff" android:alpha="0.30" android:visibility="invisible" android:layout_margintop="12dp" android:layout_below="@+id/trophy2" android:layout_alignleft="@+id/trophy2" android:layout_alignstart="@+id/trophy2" android:layout_alignright="@+id/trophy2" android:layout_alignend="@+id/trophy2" /> <imageview android:id="@+id/trophy3" android:layout_width="80dp" android:layout_height="80dp" android:layout_aligntop="@+id/trophy2" android:layout_toleftof="@+id/statstrophy" android:layout_tostartof="@+id/statstrophy" app:srccompat="@drawable/trophy3" android:visibility="invisible"/> <button android:id="@+id/enablepopup3" android:layout_width="60dp" android:layout_height="30dp" android:background="@drawable/bg_pill_trophy" android:text="info" android:textcolor="#ffffff" android:alpha="0.30" android:visibility="invisible" android:layout_alignbottom="@+id/enablepopup2" android:layout_alignleft="@+id/trophy3" android:layout_alignstart="@+id/trophy3" android:layout_aligntop="@+id/enablepopup2" android:layout_alignright="@+id/trophy3" android:layout_alignend="@+id/trophy3" /> </relativelayout> </linearlayout> <android.support.design.widget.navigationview android:layout_width="wrap_content" android:layout_height="match_parent" app:menu="@menu/navigation_menu2" android:layout_gravity="start" android:id="@+id/nv2" app:headerlayout="@layout/navigation_header" app:itemicontint="@drawable/tint_color_selector2"> </android.support.design.widget.navigationview>
try change toogle initialization. may helps you.
mtoggle = new actionbardrawertoggle(this, mdrawerlayout3, mtoolbar, r.string.open, r.string.close);
edit 1
i figured out problem. when call getsupportactionbar actionbar set. so,that means toolbar null. check toolbar id correct.
p.s. also, can call
actionbar actionbar = setsupportactionbar(mtoolbar);
because setsupportactionbar()
returns actionbar
edit 2
you don't have toolbar in xml file. add it.
Comments
Post a Comment