Use setSupportActionBar on Fragment

Asked

Viewed 131 times

-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 line setSupportActionBar(toolbar), Toolbar is probably null.

1 answer

1


I didn’t really understand the business logic, normally I would advise to use a central Toolbar on Activity and use Fragment only for the content, but following its logic, it would be like this:

v = inflater.inflate (R.layout.layout, container, false);

    Toolbar toolbar = v.findViewById(R.id.toolbar);
    ((AppCompatActivity)getActivity()).setSupportActionBar (toolbar);
    if(((AppCompatActivity)getActivity()).getSupportActionBar () != null) {
        ((AppCompatActivity) getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(false); //Mostrar o botão
        ((AppCompatActivity) getActivity()).getSupportActionBar().setHomeButtonEnabled(false);
        ((AppCompatActivity) getActivity()).setTitle ("TITULO");
    }

And if you want to use events in this same context:

toolbar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
        toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                return false;
            }
        });
  • It worked @Rodrigo only, the button wanted it to return to the main fragment, but it opens the menu.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.