0

I can try may code by no result my app install in device but can`t run it show error in LayoutInflater

public class ViewPagerAdapter extends PagerAdapter {

    private LayoutInflater l;
    public  ViewPagerAdapter(){

    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        l =(LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = l.inflate(layouts[position],container,false);
       container.addView(view);
        return view;
    }

    @Override
    public int getCount() {
        return layouts.length;
    }

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

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        View v= (View)object;
        container.removeView(v);
        }
    }
 }
Yupi
  • 4,402
  • 3
  • 18
  • 37
  • Possible duplicate of [Android getSystemService inside custom ArrayAdapter](https://stackoverflow.com/questions/4321343/android-getsystemservice-inside-custom-arrayadapter) – fweigl Dec 22 '17 at 22:42
  • 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) – Ratilal Chopda Dec 23 '17 at 02:04
  • 1
    Please share the full exception and stack trace. Right now my guess is that `layouts` is null or it's something else unrelated to this code. – Eugen Pechanec Dec 23 '17 at 10:26

3 Answers3

-1

Use LayoutInflater.from(container.getContext()) or container.getContext().getSystemService() to obtain your inflater instance

elmorabea
  • 3,243
  • 1
  • 14
  • 20
  • `ViewPagerAdapter` looks like an inner class otherwise he wouldn't be able to call `getSystemService` directly. NPE comes probably from somewhere else. – Eugen Pechanec Dec 23 '17 at 10:26
-1

You can access the system services through context like below code

LayoutInflater vi = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);

You should pass the context to the ViewPagerAdapter through constructor, through which you can access the systemServices to get the inflater.

Durga M
  • 534
  • 5
  • 17
  • `ViewPagerAdapter` looks like an inner class otherwise he wouldn't be able to call `getSystemService` directly. NPE comes probably from somewhere else. – Eugen Pechanec Dec 23 '17 at 10:23
-2

Initialise your Inflater using,

  LayoutInflater inflater = getLayoutInflater().from(context);