Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

android.view.View android.view.View.findViewById(int)' on a null How to fix this error

I don’t know how to fix this error.Repeat findViewById as a null pointer because java.lang.NullPointerException: Attempt to invoke virtual method ‘android.view.View android.view.View.findViewById(int)’ on a null object reference I have given the image clearly below, see it and fix the error i am new devloper enter image description here

  public class FragmentCategory extends Fragment {
    private Call<CallbackHome> callbackCall = null;
    SharedPref sharedPref;
    private View root_view;
    private GalleryAdapter adapterCategory;
    private GridView gridView;
    private List<Category> mensWears;
    private GalleryAdapter adapter;

    public FragmentCategory() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater,ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_phones,container,false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view,savedInstanceState);

        this.callbackCall = RestAdapter.createAPI(getApiUrl).getHome(AppConfig.REST_API_KEY);
        this.callbackCall.enqueue(new Callback<CallbackHome>() {
            public void onResponse(Call<CallbackHome> call, Response<CallbackHome> response) {
                CallbackHome responseHome = response.body();
                if (responseHome == null || !responseHome.status.equals("ok")) {

                    return;
                }
                displayData(responseHome);


            }

            private void displayData(CallbackHome responseHome) {

                displayCategory(responseHome.category);


            }


            public void onFailure(Call<CallbackHome> call, Throwable th) {
                Log.e("onFailure", th.getMessage());
                if (!call.isCanceled()) {

                }
            }
        });



    }

    private void displayCategory(List<Category> list) {
        GridView gridView = (GridView) root_view.findViewById(R.id.gridHolder);
        gridView.setAdapter(adapterCategory);

       
        
        adapterCategory = new GalleryAdapter(getActivity(), new ArrayList<>());
        gridView.setAdapter(adapterCategory);

        LinearLayout lyt_category = root_view.findViewById(R.id.lyt_category);
        if (list.size() > 0) {
            lyt_category.setVisibility(View.VISIBLE);
        } else {
            lyt_category.setVisibility(View.GONE);
        }
    }

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Replace your onCreateView code with this

@Override
public View onCreateView(LayoutInflater inflater,ViewGroup container,
                     Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    root_view = inflater.inflate(R.layout.fragment_phones,container,false);
    return root_view
}

You forgot to initialize your instance variable private View root_view;

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading