0

First of all, I am absolute beginner for this, I'm sorry if this such a dumb mistakes. But I always got this error

java.lang.NullPointerException: Attempt to invoke virtual method 'void androidx.recyclerview.widget.RecyclerView.setLayoutManager(androidx.recyclerview.widget.RecyclerView$LayoutManager)' on a null object referenceat com.example.loginregister.ui.home.HomeFragment$1.onResponse(HomeFragment.java:104)

whenever I tried to run my app. I already changed getActivity() to HomeFragment.this , or getApplicationActivity() but still error, I'm stuck this for a long time because I not really understand how it's work, I hope someone can explain to me how to call context in fragment, here my java

public class HomeFragment extends Fragment {

    private RecyclerView rvData;
    private RecyclerView.Adapter adData;
    private RecyclerView.LayoutManager lmData;
    private List<DataModel> listData = new ArrayList<>();


    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {


        rvData = getActivity().findViewById(R.id.rv_user);
        lmData = new LinearLayoutManager(getActivity(),LinearLayoutManager.VERTICAL,false);
        rvData.setLayoutManager(lmData);
        retrieveData();

        return inflater.inflate(R.layout.fragment_home, container, false);
    }

    public void retrieveData(){

        APIRequestData ardData = RetroServer.connectRetrofit().create(APIRequestData.class);
        Call<ResponseModel> showData = ardData.ardRetrieveData();

        showData.enqueue(new Callback<ResponseModel>() {
            @Override
            public void onResponse(Call<ResponseModel> call, Response<ResponseModel> response) {
                int code = response.body().getCode();
                String message = response.body().getMessage();

                Toast.makeText(getActivity(), "Code: "+code+"| Message: "+message, Toast.LENGTH_SHORT).show();
                listData = response.body().getData();

                adData = new AdapterData(getActivity(),listData);
                rvData.setLayoutManager(lmData);
                adData.notifyDataSetChanged();
            }

            @Override
            public void onFailure(Call<ResponseModel> call, Throwable t) {
                Toast.makeText(getActivity(), "Fail to Retrieve Data", Toast.LENGTH_SHORT).show();
            }
        });

    }

and here is my fragment_home.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".ui.home.HomeFragment">


    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/rv_user"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:listitem="@layout/card_item"
        />


</androidx.constraintlayout.widget.ConstraintLayout>

  • If the RecyclerView is in fragment layout,, you can't use `rvData = getActivity().findViewById(R.id.rv_user)` as it looks in the activity layout – Zain Jul 12 '21 at 14:39
  • 1
    If it's in the fragment layout, use: `View view = inflater.inflate(R.layout.fragment_home, container, false); rvData = view.findViewById(R.id.rv_user);` – Zain Jul 12 '21 at 14:40

2 Answers2

2

use this code

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_home, container, false);
    rvData = view.findViewById(R.id.rv_user);
    return view;
}
omid
  • 86
  • 3
1

As I see your layout is supposedly layout for a fragment. You should edit your code like this:

   @Override 
   public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View inflate = inflater.inflate(R.layout.fragment_home.xml, container, false);

        rvData = inflate.findViewById(R.id.rv_user);
        lmData = new LinearLayoutManager(getActivity(),LinearLayoutManager.VERTICAL,false);
        rvData.setLayoutManager(lmData);
        retrieveData();

        return inflate;
    }

If you really need your "context" basically the private fragment variable

private Context context;

Then you may retrieve it in onAttach method somewhat like this:

    @Override
    public void onAttach(@NonNull Context context) {
        super.onAttach(context);

        this.context = context;
    }

This enables you to use context specific features like getting colors etc.

RickertBrandsen
  • 201
  • 4
  • 15