PhDeOliveira
PhDeOliveira

Reputation: 2333

onClick inside fragment giving me NPE

I've been wrestling with this for quite a while now.

I switched from having a regular Activity to now using fragments (by users recommendations), but I've been getting the same issue.

I have 3 pages that are now 3 fragments inside of a ViewPager. I want the button with the id addSiteButton in the addSite fragment; when clicked, to scroll over to setCurrentItem(2).

Any help is appreciated! Here's my code.

The FragmentActivity

public class fieldsActivity extends FragmentActivity {


private static final int NUMBER_OF_PAGES = 3;

ViewPager mPager;
MyFragmentPagerAdapter mMyFragmentPagerAdapter;


/**
 * Called when the activity is first created.
 */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // to create a custom title bar for activity window
    requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);

    setContentView(R.layout.fields);
    // use custom layout title bar
    getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.topbar);

    mPager = (ViewPager) findViewById(R.id.fieldspager);
    mMyFragmentPagerAdapter = new MyFragmentPagerAdapter(getSupportFragmentManager());
    mPager.setAdapter(mMyFragmentPagerAdapter);
    mPager.setCurrentItem(1);


}


private static class MyFragmentPagerAdapter extends FragmentPagerAdapter {

    public MyFragmentPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int page) {

        switch (page) {

            case 0: return new settingFields();

            case 1: return new addSite();

            case 2: return new createSite();

        }

       return null;


    }

    @Override
    public int getCount() {

        return NUMBER_OF_PAGES;
    }

}

The class with the button

public class addSite extends Fragment {


public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment

    if (container == null) {
        return null;
    }

    RelativeLayout mRelativeLayout = (RelativeLayout) inflater.inflate(R.layout.add_site, container, false);
    final ViewPager mPager = (ViewPager) mRelativeLayout.findViewById(R.id.fieldspager);
    Button addSiteButton = (Button) mRelativeLayout.findViewById(R.id.addSiteButton);
    addSiteButton.setOnClickListener(new View.OnClickListener() {


        @Override
        public void onClick(View view) {

            mPager.setCurrentItem(2, true);

        }
    });

   return mRelativeLayout;

}
}

I feel like I'm going in circles and not getting the desired motion.

Thanks for the help!

Upvotes: 1

Views: 501

Answers (1)

A--C
A--C

Reputation: 36449

Simply make the Fragment as a class inside the FragmentActivity and take out the variable shadowing in onCreateView() since your pager is a global variable.

public class fieldsActivity extends FragmentActivity {


  private static final int NUMBER_OF_PAGES = 3;

  ViewPager mPager;
  MyFragmentPagerAdapter mMyFragmentPagerAdapter;


  /**
   * Called when the activity is first created.
   */
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // to create a custom title bar for activity window
    requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);

    setContentView(R.layout.fields);
    // use custom layout title bar
    getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.topbar);

    mPager = (ViewPager) findViewById(R.id.fieldspager);
    mMyFragmentPagerAdapter = new MyFragmentPagerAdapter(getSupportFragmentManager());
    mPager.setAdapter(mMyFragmentPagerAdapter);
    mPager.setCurrentItem(1);


  }

  public static class addSite extends Fragment {


    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

      RelativeLayout mRelativeLayout = (RelativeLayout) inflater.inflate(R.layout.add_site, container, false);
      //final ViewPager mPager = (ViewPager) mRelativeLayout.findViewById(R.id.fieldspager);
      Button addSiteButton = (Button) mRelativeLayout.findViewById(R.id.addSiteButton);
      addSiteButton.setOnClickListener(new View.OnClickListener() {


        @Override
        public void onClick(View view) {

        mPager.setCurrentItem(2, true);

        }
      });

      return mRelativeLayout;

    }
  }
}

Also consider using the @Override annotation whenever you override a method so you don't make mistakes and use proper naming conventions. Java classes start with a capital.

Upvotes: 2

Related Questions