0

I tried to convert Activity into a Fragment.

Cant seem to correctly pass the context ( marked the corresponding lines). Android Studio show that marked lines seem to be unreachable .

  1. mRecyclerView = getView().findViewById(R.id.recycler_view); // <<<<<

  2. mExampleAdapter = new SearchItemAdapter(SearchFragment.this, mExampleList); //<<<< ( this is from inside a method)

Any help is appriciated !

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


    mRecyclerView = getView().findViewById(R.id.recycler_view); // <<<<<
    mRecyclerView.setHasFixedSize(true);
    mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));


This is another part inside a method with same problem :

mExampleAdapter = new SearchItemAdapter(SearchFragment.this, mExampleList); //<<<<
Medy
  • 55
  • 1
  • 6
  • The `return` statement should be the last line in that method. Once you `return`, execution leaves the method, which is what your error message means. Also, you cannot use `getView()` in `onCreateView()`, as it's what creates the `View` for `getView()`. If you want to find your `View`s in `onCreateView()`, follow the simple example in [the answer here](https://stackoverflow.com/a/35787883). – Mike M. Mar 01 '18 at 00:01
  • Thx that helped . Though I still cant get the context for the adapter to work. – Medy Mar 01 '18 at 02:03
  • A `Fragment` is not a `Context`, so you can't use `SearchFragment.this` for one. Use `getActivity()` instead. – Mike M. Mar 01 '18 at 02:04

0 Answers0