I'm trying to start a slideshow once a start button is pressed. I have seen a few examples using ViewPager and PagerAdapter on here, but none of them seem to help. My code is below. When I press the start button, my logCat is saying there is a nullPointerException.
public class MainActivity extends Activity {
private ViewPager mViewPager;
private SwipeAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void startShow(View v) {
Toast toast = Toast.makeText(this, "Button was Clicked!", Toast.LENGTH_LONG);
toast.show();
mViewPager = (ViewPager) findViewById(R.id.pager);
adapter = new SwipeAdapter(MainActivity.this);
mViewPager.setAdapter(adapter);
}
private class SwipeAdapter extends PagerAdapter {
private LayoutInflater mInflater;
private int[] mLayouts = {R.layout.layout1, R.layout.layout2, R.layout.layout3, R.layout.layout4};
public SwipeAdapter(Context context) {
mInflater = LayoutInflater.from(context);
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View) object);
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
ViewGroup pageView = (ViewGroup) mInflater.inflate(mLayouts[position],
container, false);
container.addView(pageView);
getItemPosition(pageView);
return pageView;
}
@Override
public int getCount() {
return mLayouts.length;
}
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0 == arg1;
}
}
}
My button contains an onClick attribute that works fine. My 4 layout files contain ImageViews that contain the 4 pictures for the slideshow (defined: R.layout.layout1, etc...). I have a pager xml file that simply defines the ViewPager. Any suggestions?