-1
I have an app that uses NavigationDrawer
, I need to change the name of toolbar
for each fragment.
My MainActivity
extension of AppCompatActivity
then I can change the fragment name using:
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
toolbar.setTitle("Nome Fragmento");
Except in my main fragment I have buttons that also calls fragmentos
, and I need their names changed, but I can’t use the setSupportActionBar(toolbar);
I saw somewhere people using ((AppCompatActivity) getActivity()).setSupportActionBar(toolbar);
but also can’t, it opens the application and when I click the app closes and gives the following error in Logcat:
05-03 15:16:30.305 14847-14847/com.example.expres E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.NullPointerException
at com.example.expres.fragmentos.principal$1.onClick(principal.java:95)
at android.view.View.performClick(View.java:4476)
at android.view.View$PerformClick.run(View.java:18795)
at android.os.Handler.handleCallback(Handler.java:730)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:176)
at android.app.ActivityThread.main(ActivityThread.java:5493)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1225)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1041)
at dalvik.system.NativeStart.main(Native Method)
I really need help.
I don’t know much about Android Studio, but
NullPointerException
appears when trying to access some null field/method. If the error occurs on this linesetSupportActionBar(toolbar)
, Toolbar is probably null.– Focos