0

I'm trying to set onClick listener to a button from a ViewPager's page but when I try to run the app I get NullPointerException

....

public class MainActivity extends Activity {
    private ViewPager vPager;
    private MyPagerAdapter pagerAdapter;
    private TextView tv;
    private MyViewPagerListener pagerListener;
    private Button deleteAll;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        tv=(TextView) findViewById(R.id.page_state);

        pagerListener = new MyViewPagerListener();
        pagerAdapter = new MyPagerAdapter();
        vPager = (ViewPager) findViewById(R.id.vPager);
        vPager.setAdapter(pagerAdapter);
        vPager.setCurrentItem(0);
        vPager.setOnPageChangeListener(pagerListener);
        //Here is the problem
        ClickListener cl = new ClickListener();
        deleteAll = (Button) vPager.findViewById(R.id.btnDelete);
        deleteAll.setOnClickListener(cl);
    }

    .....

}
Krisztian
  • 373
  • 1
  • 3
  • 15
  • 5
    Please show full stack trace. – eleven Mar 25 '14 at 20:50
  • Setting the onClickListener of any view within a given page should be done in the adapter, not in onCreate() – Dallas Mar 25 '14 at 20:54
  • this `ClickListener cl = new ClickListener();` is the line that throws NPE?!? How can that be? please post full stacktrace! – donfuxx Mar 25 '14 at 21:00
  • i think the problem is this: `vPager.setOnPageChangeListener(pagerListener);` – Marco Acierno Mar 25 '14 at 21:00
  • yeah I guess so, because `ClickListener cl = new ClickListener();` cannot throw NPE. the `//Here is the problem` code-comment is certainly not correctly placed – donfuxx Mar 25 '14 at 21:02
  • Maybe the view that you are referencing is not the view that the ViewPager has as its current item and thus and app cannot find the deleteAll button and causing a NullPointerException – kabuto178 Mar 25 '14 at 21:02
  • As far as I understand, You try to find button (R.id.btnDelete) inside the ViewPager, but not in the current fragment, so you are getting null on finding. You should do everything dedicated to views initialization inside fragment's onCreateView(). – Tzoiker Mar 25 '14 at 21:03
  • Are you positive you have views called `vPager` and `btnDelete` in your `activity_main` layout? If not, either `vPager.setAdapter(...)` or `deleteAll.setOnClickListener(...)` will throw a NPE. – Yjay Mar 25 '14 at 21:03
  • possible duplicate of [What is a Null Pointer Exception?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception) – donfuxx Mar 25 '14 at 21:06
  • 2
    i think without a response from him, we could say everything can cause a NPE here. – Marco Acierno Mar 25 '14 at 21:06
  • http://pastebin.com/21GxgpDB Here is the main activity. http://pastebin.com/uYuDQT1g this is the adapter's code – Krisztian Mar 26 '14 at 14:26

1 Answers1

-1

You should register a new OnClickListener object instead of a ClickListener. Also, you have to provide an implementation for the onClick method like this:

new OnClickListener() {
public void onClick(View v) {
  // do something when the button is clicked
}
};