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 .