1

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!

Community
  • 1
  • 1
cgr
  • 4,578
  • 2
  • 28
  • 52

2 Answers2

1

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

marcinj
  • 48,511
  • 9
  • 79
  • 100
  • Thanks for answer @Marcin, I added getWindow().requestFeature(Window.FEATURE_ACTION_BAR); in activity before super.onCreate(). And removed from fragment. But getActivity().getActionBar() returned null. java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context android.app.ActionBar.getThemedContext()' on a null object reference – cgr Dec 02 '15 at 17:11
  • @cgr I am not sure but onCreateOptionsMenu might be called before fragment gets attached to activity, you might check if its null and use it only when its not null. To force recreating of options menu call getActivity().supportInvalidateOptionsMenu() inside for example onCreateView. Also check if you should acually call getSupportActionBar. – marcinj Dec 02 '15 at 17:19
  • Yes, I had to call getSupportActionBar(). I got error in next line while getting the spinner using getActionView..it was returning null. So I changed android:actionLayout in menu_reports to app:actionLayout so it worked. Thanks! – cgr Dec 02 '15 at 21:41
0

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.

Jörn Buitink
  • 2,906
  • 2
  • 22
  • 33
  • Thanks for answer @jörn, I am sorry wait. I added getWindow().requestFeature(Window.FEATURE_ACTION_BAR); in in activity before super.onCreate(). And removed from fragment. But getActivity().getActionBar() returned null. java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context android.app.ActionBar.getThemedContext()' on a null object reference – cgr Dec 02 '15 at 17:05