0
@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        listView=container.findViewById(R.id.listview);
       // View view= inflater.inflate(R.layout.fragment_home_page, container, false);
        View v = getActivity().getLayoutInflater().inflate(R.layout.fragment_home_page,null);
        final HomePageListAdapter homePageListAdapter=new HomePageListAdapter(container.getContext(),productName,sku,type,profile,getLayoutInflater());
        listView.setAdapter(homePageListAdapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
               Toast.makeText(getContext(),productName[position],Toast.LENGTH_SHORT).show();
            }
        });

        return v;
    }

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setOnItemClickListener(android.widget.AdapterView$OnItemClickListener)' on a null object reference at com.example.webcreta.ui.home.HomePage.HomePageFragment.onCreateView(HomePageFragment.java:42)

Ashish
  • 6,791
  • 3
  • 26
  • 48
  • 6
    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) – a_local_nobody Oct 23 '19 at 13:11
  • 3
    Change `listView=container.findViewById(R.id.listview);` to `listView=v.findViewById(R.id.listview);` Move that line after `View v = getActivity().getLayoutInflater().inflate(R.layout.fragment_home_page,null);` – Piyush Oct 23 '19 at 13:12

2 Answers2

2

First Inflate View then Intialize

try this code:

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment

       // View view= inflater.inflate(R.layout.fragment_home_page, container, false);
        View v = getActivity().getLayoutInflater().inflate(R.layout.fragment_home_page,null);
       listView=v.findViewById(R.id.listview);
        final HomePageListAdapter homePageListAdapter=new HomePageListAdapter(container.getContext(),productName,sku,type,profile,getLayoutInflater());
        listView.setAdapter(homePageListAdapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
               Toast.makeText(getContext(),productName[position],Toast.LENGTH_SHORT).show();
            }
        });

        return v;
    }
Jahanvi Kariya
  • 377
  • 3
  • 14
0

Try this code

View view;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    view = inflater.inflate(R.layout.fragment_home_page, container, false);
    listView = view.findViewById(R.id.listview);    
    final HomePageListAdapter homePageListAdapter=new HomePageListAdapter(container.getContext(),productName,sku,type,profile,getLayoutInflater());
    listView.setAdapter(homePageListAdapter);
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Toast.makeText(getContext(),productName[position],Toast.LENGTH_SHORT).show();
        }
    });
    return view;
}

Hope it will help for you

gpuser
  • 1,143
  • 1
  • 9
  • 6