这是第二个类,它为我使用的图像提供格式
public ViewPagerAdapter(Activity activity, String[] images) {
this.activity = activity;
this.images = images;
}
@Override
public int getCount() {
return images.length;
}
@Override
public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
return view==object;
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
inflater =(LayoutInflater)activity.getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View itemView= inflater.inflate(R.layout.viewpager_item,container,false);
ImageView image;
DisplayMetrics dis = new DisplayMetrics();
activity.getWindowManager().getDefaultDisplay().getMetrics(dis);
int height = dis.heightPixels;
int width = dis.widthPixels;
image.setMinimumHeight(height);
image.setMinimumWidth(width);
try {
Picasso.with(activity.getApplicationContext())
.load(images[position])
.placeholder(R.mipmap.ic_launcher)
.error(R.mipmap.ic_launcher)
.into(image);
}
catch (Exception ex) {
}
container.addView(itemView);
return itemView;
}
@Override
public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
((ViewPager) container).removeView((View)object);
}}`
这是主要的课程
ViewPager viewPager;
ViewPagerAdapter adapter;
private String [] images = {
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search);
viewPager = (ViewPager)findViewById(R.id.viewpager);
adapter = new ViewPagerAdapter(Search.this,images);
viewPager.setAdapter(adapter);
}}
我正在关注这个youtube教程,我有相同的代码,但我的代码不起作用https://www.youtube.com/watch?v=SX8l9vv-N_4
如果强迫我将变量声明为null,但在视频中没有。
答案 0 :(得分:0)
你忘记了findViewById image = findViewById(...);