0

This is my Pageradapter :

private class MyAdapter extends PagerAdapter {

            Context context;

            public MyAdapter(Context context) {
                super();
                this.context = context;
            }

            @Override
            public Object instantiateItem(ViewGroup container, int position) {
                ModelObject modelObject = ModelObject.values()[position];
                LayoutInflater inflater = LayoutInflater.from(context);
                ViewGroup layout = (ViewGroup) inflater.inflate(modelObject.getLayoutResId(), container, false);
                TextView bottomTitle = (TextView)layout.findViewById(R.id.bottom_title);
                ImageView topImageView =(ImageView) layout.findViewById(R.id.top_image_view);
                TextView topTitleTextview=(TextView)layout.findViewById(R.id.top_title_text_view);
                TextView bottomDescriptionTextView=(TextView)layout.findViewById(R.id.bottom_description_textview);
                bottomTitle.setText(pointsHelpItemDetailViewModel.getHelpItem().subtitle);
                int resId = getContext().getResources().getIdentifier(pointsHelpItemDetailViewModel.getHelpItem().image, "drawable", context.getPackageName());
                topImageView.setBackgroundResource(resId);
                topTitleTextview.setText(pointsHelpItemDetailViewModel.getHelpItem().subtitle);
                bottomDescriptionTextView.setText(pointsHelpItemDetailViewModel.getHelpItem().content);
                container.addView(layout);
                return layout;
            }

            @Override
            public void destroyItem(ViewGroup collection, int position, Object view) {
                collection.removeView((View) view);
            }

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

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

            @Override
            public CharSequence getPageTitle(int position) {
                ModelObject customPagerEnum = ModelObject.values()[position];
                return context.getString(customPagerEnum.getTitleResId());
            }
        }

this is my ModelObject

public enum ModelObject {

    RED(R.color.tesco_red, R.layout.point_help_image_view),
    BLUE(R.color.tesco_blue, R.layout.point_help_decription_view);

    private int mTitleResId;
    private int mLayoutResId;

    ModelObject(int titleResId, int layoutResId) {
        mTitleResId = titleResId;
        mLayoutResId = layoutResId;
    }

    public int getTitleResId() {
        return mTitleResId;
    }

    public int getLayoutResId() {
        return mLayoutResId;
    }
}

i have added two layout point_help_image_view,point_help_decription_view in first layout I have one ImageView and one TextView and in second layout two text view i want to access.

I am trying to access using given code but when i try to setText i am getting exception java.lang.NullPointerException: Attempt to invoke virtual method 'void android.view.View.setBackgroundResource(int)' on a null object reference . please help me how to access view in pager adapter .

Tejas Pandya
  • 3,987
  • 1
  • 26
  • 51
MARSH
  • 117
  • 1
  • 7
  • 22
  • Try this https://stackoverflow.com/questions/32522489/nullpointerexception-on-using-setbackgroundresource – Masum Mar 02 '19 at 03:38
  • but how this inside public Object instantiateItem(@NonNull ViewGroup container, int position) { when i use ur solution its showing null in textview initialisation – MARSH Mar 02 '19 at 03:42
  • Why show null?? you can see and hope you fix it. https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it – Masum Mar 02 '19 at 03:44
  • i need to access two two layout view so that i can display it some text or image please check my question – MARSH Mar 02 '19 at 03:49
  • @ Masum any suggestion for this issue because not able to resolve this issue – MARSH Mar 02 '19 at 06:16
  • I think at first you implement this. Then try a new one. May help this. https://www.journaldev.com/10096/android-viewpager-example-tutorial – Masum Mar 02 '19 at 06:31
  • yes i have use same @Masum can you please suggest me in this example how to print blue and red layout textview programmatically ? – MARSH Mar 02 '19 at 07:10

1 Answers1

0

Try this

@NonNull
    @Override
    public Object instantiateItem(@NonNull ViewGroup container, final int position) {
        ModelObject modelObject = ModelObject.values()[position];
        View itemView = LayoutInflater.from(container.getContext()).inflate(modelObject.getLayoutResId(), container, false);

        TextView bottomTitle = (TextView) itemView.findViewById(R.id.bottom_title);
        bottomTitle.setText("MARSH");

        return itemView;
    }
VIISHRUT MAVANII
  • 11,410
  • 7
  • 34
  • 49