-2

I'm trying to create a RecyclerView inside Fragment. The problem is when I pass context I catch NullPointerException. Here is my code in Fragment:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    getActivity().setTitle(getString(R.string.title_services));

    String name = "Ivan";
    Double price = 25.0;
    String duration = "23:30";
    String description = "ahjdbsjdhask skdfksd";
    items = new ArrayList<>();

    for (int i = 0; i < 15; i++) {
        items.add(new Service(name, price, duration, description));
    }

    frameLayout = container.findViewById(R.id.layout_snack);
    recyclerView = container.findViewById(R.id.service_list);
    itemsAdapter = new ServiceAdapter(R.layout.service_item, items);
    fab = container.findViewById(R.id.fab_service);

    recyclerView.setLayoutManager(new 
    LinearLayoutManager(getActivity().getBaseContext()));
    recyclerView.setItemAnimator(new DefaultItemAnimator());

    recyclerView.setAdapter(itemsAdapter);
    return inflater.inflate(R.layout.fragment_service, container, false);
}

and here is my exception:

E/AndroidRuntime: FATAL EXCEPTION: main
              Process: me.ibanha.bizmanager, PID: 3804
              java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.RecyclerView.setLayoutManager(android.support.v7.widget.RecyclerView$LayoutManager)' on a null object reference
                  at me.ibanha.bizmanager.fragment.ServiceFragment.onCreateView(ServiceFragment.java:54)
                  at android.support.v4.app.Fragment.performCreateView(Fragment.java:2343)
                  at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1421)
                  at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1752)
                  at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1821)
                  at android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:797)
                  at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2595)
                  at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2382)
                  at android.support.v4.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2337)
                  at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2244)
                  at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:702)
                  at android.os.Handler.handleCallback(Handler.java:790)
                  at android.os.Handler.dispatchMessage(Handler.java:99)
                  at android.os.Looper.loop(Looper.java:164)
                  at android.app.ActivityThread.main(ActivityThread.java:6519)
                  at java.lang.reflect.Method.invoke(Native Method)
                  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:440)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)

the problem in recyclerView.setLayoutManager(new LinearLayoutManager(getActivity().getBaseContext()));

I don't know how to pass context in Layout Manager. Sorry for my bad English.

Ivan Banha
  • 753
  • 2
  • 12
  • 23
  • Possible duplicate of [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – ADM Mar 05 '18 at 15:15
  • You did not initialize view for fragment . First search for How to create a fragment . – ADM Mar 05 '18 at 15:17

1 Answers1

1

You should Declare View in top of the onCreateView

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View view=inflater.inflate(R.layout.fragment_service, container, false);
    getActivity().setTitle(getString(R.string.title_services));

    String name = "Ivan";
    Double price = 25.0;
    String duration = "23:30";
    String description = "ahjdbsjdhask skdfksd";
    items = new ArrayList<>();

    for (int i = 0; i < 15; i++) {
        items.add(new Service(name, price, duration, description));
    }

    frameLayout = view.findViewById(R.id.layout_snack);
    recyclerView = view.findViewById(R.id.service_list);
    itemsAdapter = new ServiceAdapter(R.layout.service_item, items);
    fab = view.findViewById(R.id.fab_service);

    recyclerView.setLayoutManager(new 
    LinearLayoutManager(getActivity().getBaseContext()));
    recyclerView.setItemAnimator(new DefaultItemAnimator());

    recyclerView.setAdapter(itemsAdapter);
    return view;
}
Nikunj Paradva
  • 15,332
  • 5
  • 54
  • 65