0

I have a runtime error. got this error message:

java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.LayoutInflater.inflate(int, android.view.ViewGroup, boolean)' on a null object reference

studio is pointing at this row:

View view= lI.inflate(R.layout.fragment_frag3,container,false);

public class Frag3 extends Fragment {

    ViewPager viewPager;
    CardAdapter2 adapter2;
    List<String> cardWords;
    LayoutInflater lI;

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

        View view= lI.inflate(R.layout.fragment_frag3,container,false);
        viewPager= (ViewPager)view.findViewById(R.id.vp);
        viewPager.setAdapter(adapter2);
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        cardWords= new ArrayList<>();
        cardWords.add("x");
        cardWords.add("y");
        cardWords.add("z");

        adapter2= new CardAdapter2(getActivity(),cardWords);

        // Inflate the layout for this fragment
        return view;
    }


}

Adapter:

public class CardAdapter2 extends PagerAdapter {
    private Context mContext;
    private List<String> cardWords;
    private LayoutInflater layoutInflater;

    public CardAdapter2(Context mContext, List<String> cardWords) {
        this.mContext = mContext;
        this.cardWords = cardWords;
    }

    @Override
    public int getCount() {
        return cardWords.size();
    }

    @NonNull
    @Override
    public Object instantiateItem(@NonNull ViewGroup container, int position) {
        layoutInflater= LayoutInflater.from(mContext);
        View view= layoutInflater.inflate(R.layout.card_design2,container,false);

        TextView title= view.findViewById(R.id.textView2);
        title.setText(cardWords.get(position));
        container.addView(view,0);

        return super.instantiateItem(container, position);
    }

    @Override
    public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
        container.removeView((View)object);
    }

    @Override
    public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
        return view.equals(object);
    }


}
Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
Merve K.
  • 37
  • 1
  • 6

1 Answers1

0

You define a class variable LayoutInflater

LayoutInflater lI;

at the top of your Frag3 class. Then in your onCreateView method, you call inflate on that uninitialized inflater. You are passed in a layout inflater already, so the change you need to make is

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

Just call inflate on the LI that is passed to you

Tyler
  • 955
  • 9
  • 20