Nir Doshi
Nir Doshi

Reputation: 35

Android: RecyclerView nullpointer exception

I have created recycler view and I have imported all necessary dependency in the Gradle file. my target SDK is 29.so all my dependency is having that androidx well in my layout file I have also created id of"list" but I don't know why it is showing error. thank you in advance.

package com.napps.wallpaper;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

public class fragment_wallpaper extends Fragment {

    RecyclerView recyclerView;
    RecyclerView.LayoutManager layoutManager;
    RecyclerView.Adapter adapter;
    View view;

    public fragment_wallpaper(){

    }

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

        View view= inflater.inflate(R.layout.fragment_wallpaper,container,false);
        return  view;

    }


    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);


        recyclerView=view.findViewById(R.id.list); //this where i am getting error
        recyclerView.setHasFixedSize(true);

        layoutManager=new LinearLayoutManager(this.getActivity());
        recyclerView.setLayoutManager(layoutManager);

        adapter=new imageAdapter(this.getActivity(),array_class.arrayurl);
        recyclerView.setAdapter(adapter);

    }

}



Upvotes: 0

Views: 60

Answers (1)

Aqsa Shahid
Aqsa Shahid

Reputation: 64

Try this
Baically you are not initializing your View you are creating a new variable which is causing the trouble. Hope it helps. Happy Coding :)

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

            view= inflater.inflate(R.layout.fragment_wallpaper,container,false);
            return  view;

        }

Upvotes: 1

Related Questions