Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

getActionBar() is undefined

I want to add a ActionBar to my Fragment class, but I keep getting errors. This is my class:

public class TaskDetailFragment extends Fragment {

    public static final String ARG_ITEM_ID = "item_id";

    MyTasks.taskItem mItem;

    public TaskDetailFragment() {

    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if (getArguments().containsKey(ARG_ITEM_ID)) {
            mItem = MyTasks.ITEM_MAP.get(getArguments().getString(ARG_ITEM_ID));
        }
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.fragment_task_detail,
                container, false);

        ActionBar actionBar = getActionBar();
        actionBar.show();


        if (mItem != null) {
            //Add TexViews
            ((TextView) rootView.findViewById(R.id.in_Name))
                    .setText(MyTasks.customers[(Integer.parseInt(mItem.id))][1]);
            ((TextView) rootView.findViewById(R.id.in_Address))
                    .setText(MyTasks.customers[(Integer.parseInt(mItem.id))][3] + "  " + MyTasks.customers[(Integer.parseInt(mItem.id))][2]);

        }
        return rootView;
    }
}

This is my error:

The method getActionBar() is undefined for the type TaskDetailFragment

like image 666
ObAt Avatar asked Dec 16 '22 18:12

ObAt


1 Answers

As far as I know, you need to ask the Activity (to which the Fragment has been attached) for the ActionBar:

ActionBar actionBar = getActivity().getActionBar();

Or if you're using the support library and/or ActionBarSherlock:

ActionBar actionBar = getFragmentActivity().getSupportActionBar();

Fragments don't define a getActionBar() method, hence the error message.

like image 128
MH. Avatar answered Dec 31 '22 11:12

MH.