android - Force Close when Adding ToolBar to Java Class -


i adding toolbar app has 3 activities.

i added toolbar first 2 activities when tried add toolbar third one, activity crashed.

when open app , go third activity (i named "profile") app crashes.

package com.mrad4tech.app.m4taudiomanager;  import android.app.actionbar;  import android.media.audiomanager; import android.os.bundle;  import android.app.activity;  import android.content.context;  import android.support.v7.app.appcompatactivity;  import android.view.menu; import android.view.menuitem;  import android.view.view;  import android.view.view.onclicklistener;  import android.widget.button; import android.widget.radiobutton;  import android.widget.radiogroup; import android.widget.textview;  import android.support.v7.widget.toolbar;  import android.widget.toast;  public class profile extends appcompatactivity {     private toolbar toolbar;     private button modebtn;     private button increasebtn;     private button decreasebtn;     private radiobutton normal;     private radiobutton silent;     private radiogroup ringgroup;     private textview status;     private audiomanager myaudiomanager;     private actionbar supportactionbar;      @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);          setcontentview(r.layout.activity_profile);            normal = (radiobutton) findviewbyid(r.id.radionormal);         silent = (radiobutton) findviewbyid(r.id.radiosilent);         status = (textview) findviewbyid(r.id.text);         ringgroup = (radiogroup) findviewbyid(r.id.radioringer);          modebtn = (button) findviewbyid(r.id.mode);         modebtn.setonclicklistener(new onclicklistener() {              @override             public void onclick(view v) {                 // todo auto-generated method stub                 int selectedid = ringgroup.getcheckedradiobuttonid();                  // find radiobutton checked id                 if (selectedid == silent.getid()) {                     silentenable(v);                 } else if (selectedid == normal.getid()) {                     normalenable(v);                 } else {                     vibrateenable(v);                 }             }         });          increasebtn = (button) findviewbyid(r.id.increase);         increasebtn.setonclicklistener(new onclicklistener() {              @override             public void onclick(view v) {                 // increase volume , show ui                 myaudiomanager.adjustvolume(audiomanager.adjust_raise, audiomanager.flag_show_ui);                 toast.maketext(getapplicationcontext(), "increase volume",                         toast.length_short).show();             }         });          decreasebtn = (button) findviewbyid(r.id.decrease);         decreasebtn.setonclicklistener(new onclicklistener() {              @override             public void onclick(view v) {                 // decrease volume , show ui                 myaudiomanager.adjustvolume(audiomanager.adjust_lower, audiomanager.flag_show_ui);                 toast.maketext(getapplicationcontext(), "decrease volume",                         toast.length_short).show();             }         });         // instance of audiomanager class         myaudiomanager = (audiomanager) getsystemservice(context.audio_service);      }       public void vibrateenable(view view) {         // set ring mode vibrate         myaudiomanager.setringermode(audiomanager.ringer_mode_vibrate);         status.settext("current status: vibrate mode");     }      public void normalenable(view view) {         // set ring mode loud         myaudiomanager.setringermode(audiomanager.ringer_mode_normal);         status.settext("current status: ring mode");     }      public void silentenable(view view) {         // set ring mode silent         myaudiomanager.setringermode(audiomanager.ringer_mode_silent);         status.settext("current status: silent mode");     }   } 

in comment said,

i error when adding toolbar = (toolbar) findviewbyid(r.id.app_bar); setsupportactionbar(toolbar);

you're getting error because trying access id doesn't exists. here 've mentioned id app_bar_profile

    <include android:id="@+id/app_bar_profile"      layout="@layout/app_bar_profile"></include> 

& in code you're trying like

toolbar = (toolbar) findviewbyid(r.id.app_bar); <- app_bar id doesn't exists 

so, change id app_bar_profile

toolbar = (toolbar) findviewbyid(r.id.app_bar_profile); 

also, i'm not sure whether 've set correct layout toolbar in xml layout. need post app bar layout can examine whether you're doing mistake above.


Comments