Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't get Action Bar in fragment class

I have an activity with three fragment classes inside it. I get an error when trying to change the action bar title from inside of them. If I try to make the classes just public and not public static I get an error when I try to start that class. It should be pretty clear that the code is for preferences although that shouldn't change anything . Here's the code:

package com.simon.wikiics;  import android.preference.*; import android.os.*; import java.util.*;  public class MainSettingsActivity extends PreferenceActivity {      @Override     protected void onCreate(Bundle savedInstanceState) {     super.onCreate(savedInstanceState);  }  @Override public void onBuildHeaders(List<Header> target) {     loadHeadersFromResource(R.xml.headers, target); }  //If I don't make the classes static my app force closes when I try to start them public static class NavigationSettingsActivity extends PreferenceFragment {      @Override     public void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         addPreferencesFromResource(R.xml.navigation);         //The getActionBar() is what is giving me the error         getActionBar().setTitle("Navigation");      } }  public static class InterfaceSettingsActivity extends PreferenceFragment {      @Override     public void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         addPreferencesFromResource(R.xml.interf);         //The getActionBar() is what is giving me the error         getActionBar().setTitle("Interface");     }  }  public static class OtherSettingsActivity extends PreferenceFragment {      @Override     public void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         addPreferencesFromResource(R.xml.other);         //The getActionBar() is what is giving me the error         getActionBar().setTitle("Other");     } } } 
like image 245
SweSnow Avatar asked Aug 17 '12 22:08

SweSnow


People also ask

How do I get the activity toolbar in fragment?

getActivity()). getToolbar(); will be the right answer!! for getting the Toolbar in fragment!!

What is split action bar?

You can adapt to such changes by using split action bars, which allow you to distribute action bar content across multiple bars located below the main action bar or at the bottom of the screen. Split action bar showing action buttons at the bottom of the screen in vertical orientation.

How do I add action to action bar?

All action buttons and other items available in the action overflow are defined in an XML menu resource. To add actions to the action bar, create a new XML file in your project's res/menu/ directory. The app:showAsAction attribute specifies whether the action should be shown as a button on the app bar.

What is action bar in android?

The app bar, also known as the action bar, is one of the most important design elements in your app's activities, because it provides a visual structure and interactive elements that are familiar to users.


1 Answers

@Robert Estivil If you are using AppCompatActivity then use this:

actionBar = ((AppCompatActivity)getActivity()).getSupportActionBar(); 
like image 131
Arul Pandian Avatar answered Oct 01 '22 13:10

Arul Pandian