0

I have a fragment where I display some items in a RecyclerView. This is my XML file:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recycler_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"/>
</RelativeLayout>

And this is how I initilize it:

recyclerView = rootView.findViewById(R.id.recycler_view);

Now I have created a menu in my fragment that contains a single item:

<item android:id="@+id/search"
    android:icon="@android:drawable/ic_menu_search"
    android:title="@string/search"
    app:actionViewClass="androidx.appcompat.widget.SearchView"
    app:showAsAction="ifRoom"/>

In the onCreateOptionsMenu function, I'm only inflating the menu layout:

@Override
public void onCreateOptionsMenu(@NonNull Menu menu, @NonNull MenuInflater inflater) {
    super.onCreateOptionsMenu(menu, inflater);
    inflater.inflate(R.menu.menu_bars, menu);
}

The problem is, as soon as I click on the search button to open the edit text, I get the following warning:

Cannot scroll to position a LayoutManager set. Call setLayoutManager with a non-null argument.

How can a click on a SearchView that is not wired in any way with my RecyclerView affecte its LayoutManager? How can get rid of this warning?

Edit:

public class BarsFragment extends Fragment {
    private RecyclerView recyclerView;

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle bundle) {
        rootView = inflater.inflate(R.layout.fragment_bars, container, false);
        setHasOptionsMenu(true);
        recyclerView = rootView.findViewById(R.id.recycler_view);
        return rootView;
    }

    @Override
    public void onCreateOptionsMenu(@NonNull Menu menu, @NonNull MenuInflater inflater) {
        super.onCreateOptionsMenu(menu, inflater);
        inflater.inflate(R.menu.menu_bars, menu);
    }
}
  • Can you show us the fragment layout and code? – notihs Aug 13 at 14:44
  • Do you set any layout manager with the recycler view? – Md. Nowshad Hasan Aug 13 at 14:49
  • @notihs The fragment layout is actually the layout that is already shared. Please take a look at my fragment code in my updated question. – Lilian Sorlanski Aug 13 at 14:49
  • @Md.NowshadHasan Yes, as you can see in the layout file: app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager". – Lilian Sorlanski Aug 13 at 14:50
0

Please try adding this line on your java file.

recyclerView = rootView.findViewById(R.id.recycler_view);
LinearLayoutManager mLinearLayoutManager = new LinearLayoutManager(getActivity());
recyclerView.setLayoutManager(mLinearLayoutManager);
  • Thanks for you answer but I get the exact same behavior. – Lilian Sorlanski Aug 13 at 15:08
  • Any other thoughts? – Lilian Sorlanski Aug 13 at 17:41
  • Please google search on Cannot scroll to position a LayoutManager set. Call setLayoutManager with a non-null argument, check some blog with stack overflow. – Md. Nowshad Hasan Aug 14 at 2:00
  • Tried everything I found but without any luck :( – Lilian Sorlanski Aug 14 at 8:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.