cgr
cgr

Reputation: 4636

requestedFeature() must be called before adding content - implementing Spinner in ActionBar in Fragment

Hesitating to post this question but tried with all options available in SOF and in other sites but could not figure out why I still get

android.util.AndroidRuntimeException: requestFeature() must be called before adding content

I want to add Spinner to action bar of my Fragment. I followed Adding spinner to ActionBar and took a look at all posts related to this.

My fragment class has this:

public class MyFragment extends android.support.v4.app.Fragment {

@Override
public void onCreate(Bundle savedInstanceState) {
    setHasOptionsMenu(true);
    getActivity().getWindow().requestFeature(Window.FEATURE_ACTION_BAR);
    super.onCreate(savedInstanceState);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    return inflater.inflate(R.layout.fragment_layout, container, false);
}

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.menu_reports, menu);
    MenuItem item = menu.findItem(R.id.menuReportsSpinner);

    SpinnerAdapter spinnerAdapter = ArrayAdapter.createFromResource(getActivity().getActionBar()
            .getThemedContext(), R.array.reports_types, android.R.layout.simple_spinner_dropdown_item);

    Spinner spinner = (Spinner) MenuItemCompat.getActionView(item);
    spinner.setAdapter(spinnerAdapter);

    spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            Log.d(TAG, "Item selected from reports : " +position);
        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {
            Log.d(TAG, "No Item selected..");
        }
    });
    super.onCreateOptionsMenu(menu, inflater);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case android.R.id.home:
            Log.d(TAG, "Home memu item..");
            return true;
    }
    return super.onOptionsItemSelected(item);
}

}

fragment_layout has this:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

</LinearLayout>

menu_reports.xml has this:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
<item
    android:id="@+id/menuReportsSpinner"
    android:title="@string/reports_menu_title"
    app:showAsAction="always"
    android:actionLayout="@layout/layout_reports_menu" />
</menu>

I called requestFeature() right in onCreate() before super.onCreate(). Any hint is of helpful, thnaks!

Upvotes: 1

Views: 163

Answers (2)

J&#246;rn Buitink
J&#246;rn Buitink

Reputation: 2916

Well, you didn't call requestFeature() before adding content.

Sure, you did it in your fragment. But you request a feature from the activity window. Most likely you already set the content view in your Activity before.

Upvotes: 0

marcinj
marcinj

Reputation: 50026

You show your fragment inside some Activity, and this activity also has a layout and calls: setContentView. This error is telling you that you should call requestFeature before setContentView in your Activity.onCreate, and not Fragment.onCreate

Upvotes: 1

Related Questions