-1

I am putting ImageView inside Fragment. I have other question also but cannot find useful. this is my code:

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState)
    {
        View v = inflater.inflate(R.layout.fragment_fragment_contect,container,false);
        ImageButton imageButton = (ImageButton)getView().findViewById( R.id.ib);
        imageButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity( new Intent( getActivity(), Enquiry.class ) );
            }
        });

        return v;
    }
Paraskevas Ntsounos
  • 1,755
  • 2
  • 18
  • 34
  • 1
    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 Jun 02 '18 at 07:13
  • I provide solution of your issue, please check that out. – Sheikh Hasib Jun 02 '18 at 07:15

2 Answers2

2

You need to do like this in getview() you getting null pointer exception use view.

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState)
{
    View v = inflater.inflate(R.layout.fragment_fragment_contect,container,false);
   ImageButton imageButton = v.findViewById(R.id.ib);
    imageButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            startActivity( new Intent( getActivity(), Enquiry.class ) );
        }
    });

    return v;
}
Jigar Fumakiya
  • 2,039
  • 1
  • 8
  • 21
0

You got NullPointException because of using getview():

ImageButton imageButton = (ImageButton)getView().findViewById( R.id.ib);

Solution: use v instead of getView()

ImageButton imageButton = (ImageButton) v.findViewById( R.id.ib);

Android Fragment Referance

Sheikh Hasib
  • 7,423
  • 2
  • 25
  • 37