0

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?

  • http://stackoverflow.com/questions/17610085/how-to-switch-automatically-between-viewpager-pages – makata Jun 10 '14 at 15:27
  • @mek any idea why i'm getting a nullpointerexception? I still can't get anything to display after the button click. – israel_hill Jun 10 '14 at 15:32
  • First make sure a simple view pager is working for you. You can take a look at [Vogella's tutorial](http://www.vogella.com/code/de.vogella.android.fragment.viewpager/codestartpage.html) for this. Once you are done with it, you can refer the above link. As a note: are you able to complete the ViewPager without the button click? – makata Jun 10 '14 at 16:02

1 Answers1

0

You get NullPointerException because you are referencing pager which is not in activity_main.Place your ViewPager inside activity_main

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <android.support.v4.view.ViewPager
        android:id="@+id/pager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
         />

</LinearLayout> 

Better to move the instantiation of Views to onCreate from startShow()

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mViewPager = (ViewPager) findViewById(R.id.pager);
    adapter = new SwipeAdapter(MainActivity.this);
    mViewPager.setAdapter(adapter);
}
makata
  • 2,188
  • 2
  • 28
  • 23